我想编写一个返回用户名和密码的访问函数.这是我想出的:
#include <stdio.h>
char *
getMySQLUsername()
{
return "myUsername";
}
char *
getMySQLPassword()
{
return "myPassword";
}
int main()
{
printf("%s\n", getMySQLPassword());
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但这个代码是否正确?
我正在使用C.我正在尝试做的是一个函数,它将返回一个包含指向对象的地址的int.然后,使用我收到的int(包含地址),并构建一个指向该地址的指针.
示例:
MyClass* obj = SomeMethodReturningAPointerOfMyClass();
int address = (int) &obj;
return address;
Run Code Online (Sandbox Code Playgroud)
我认为它适用于这一部分.我得到的值是-4197276.(也许我完全错了?)
然后,另一方面,我想做的事情如下:
MyClass* obj = (MyClass*) address;
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为我无法访问任何obj方法而不会收到错误.
我找到了一种方法:
MyClass* obj = SomeMethodReturningAPointerOfMyClass();
machine->registers[2] = (int)obj;
Run Code Online (Sandbox Code Playgroud)
然后,在我收到整数的另一种方法中:
MyClass* obj = (MyClass*)address;
Run Code Online (Sandbox Code Playgroud)
我可以完美地完成对象!可能不是那么干净但它应该为它的用途做好工作!
感谢所有花时间回答的人!
如果我的类有一个可以由类客户端设置的某种指针,我该如何处理删除?
例:
class A {
};
class B {
public:
void setA(A* a) {
this->a = a;
}
private:
A* a;
};
Run Code Online (Sandbox Code Playgroud)
该类的析构函数应该如何B?应该删除a吗?正如我所看到的,用户可以通过两种方式设置此指针:
... // Assume B object b being created elsewhere
A aObj;
A* aPtr = new A();
b.setA(&aObj); // It is not OK to use delete, and class member will
// point to invalid memory location once aObj goes out
// of scope
b.setA(aPtr); // Using new will make the pointer available even after …Run Code Online (Sandbox Code Playgroud) 在C++中以这种方式将指针保存在静态分配的数组中是否合适?
void *data[1000];
Run Code Online (Sandbox Code Playgroud)
是void*32和64位机器上的各种尺寸?
我有一个函数与以下声明:
void playCard(string card);
Run Code Online (Sandbox Code Playgroud)
以及以下实施:
void Player::playCard(string card)
{
cout << "Playing " << card << "!" << endl;
// Find iterator representing the card to be played
vector<Card*>::iterator iter;
for(iter = hand.begin(); iter != hand.end(); iter++)
{
if( (*iter)->getName() == card)
continue;
}
// ERROR - Card not found in hand
if(iter == hand.end())
assert(false);
// more stuff
}
Run Code Online (Sandbox Code Playgroud)
该函数从以下代码块调用:
// Divide string into 2 words
istringstream iss(in, istringstream::in);
string command, target;
iss >> command >> target;
if(command == …Run Code Online (Sandbox Code Playgroud) 不是试图开始语法战,而只是一个随机的问题......声明指针的理想方式是什么?
int* pI = 4;
int *pI = 4;
Run Code Online (Sandbox Code Playgroud)
(或者我最喜欢的,我知道它不漂亮,但我喜欢它):
int*pI = 4;
Run Code Online (Sandbox Code Playgroud)
同样的问题代表参考:
int& rI = 4;
int &rI = 4;
Run Code Online (Sandbox Code Playgroud)
要么
int&rI = 4;
Run Code Online (Sandbox Code Playgroud)
也许没有正确的答案.同样,我应该关心是否将常量整数声明为:
const int I = 4;
Run Code Online (Sandbox Code Playgroud)
要么
int const I = 4;
Run Code Online (Sandbox Code Playgroud)
我没关心......
我喜欢通过在最后一个括号后面使用const来声明const函数的方式.
我相信常量函数具有与类似的非const函数不同的函数签名(即,const-nesss是函数签名的一部分,不像大多数来源说它只取决于参数类型和返回类型).
这是正确的吗?我应该关心吗?
呼!长标题......这里有一些伪代码来解释这个词:
int main(){
int* ptr = function1(); //the data that ptr points to is correct here
function2(ptr);
}
int function2(int* ptr){
//the data that ptr points to is still correct
int i;
for(i=0;i<length;printf("%d\n", (*ptr)[i]), i++); //since ptr points to a contiguous block of memory
function3(ptr);
}
int function3(int* ptr){
//the data that ptr points to is INCORRECT!!!
}
Run Code Online (Sandbox Code Playgroud)
为什么function3中的数据不正确?
注意:function1执行malloc()并返回指向该内存的指针.
实际代码
#include <stdlib.h>
#include <stdio.h>
//Structures
struct hash_table_data_
{
int key, data;
struct hash_table_data_ *next, *prev;
};
struct hash_table_
{ …Run Code Online (Sandbox Code Playgroud) 这是一个新手C++指针问题.我不知道为什么会发生这种情况......
我发现如果我写这段代码.这完全有效.
代码1
int *j; //create an pointer j
*j = 50; //assign 50 to the *j, meaning value pointed by address 50 is xxx
Run Code Online (Sandbox Code Playgroud)
但是,当我想尝试使它更简单.编译器给我这个错误信息.
码2
int *j = 50; //i guess this should be the same with Code1...
Run Code Online (Sandbox Code Playgroud)
编译错误
error: invalid conversion from ‘int*’ to ‘int’
Run Code Online (Sandbox Code Playgroud)
那么为什么会那样?
在标题中是否可以将多个数组连接在一起而无需复制并仅使用指针?我花费了大量的计算时间将较小的数组复制到较大的数组中.注意我不能使用向量,因为umfpack(一些矩阵求解库)不允许我或我不知道如何.
举个例子:
int n = 5;
// dynamically allocate array with use of pointer
int *a = new int[n];
// define array pointed by *a as [1 2 3 4 5]
for(int i=0;i<n;i++) {
a[i]=i+1;
}
// pointer to array of pointers ??? --> this does not work
int *large_a = new int[4];
for(int i=0;i<4;i++) {
large_a[i] = a;
}
Run Code Online (Sandbox Code Playgroud)
注意:我已经知道了一个简单的解决方案,只是迭代地将它们复制到一个新的大型数组中,但很高兴知道是否不需要复制在整个程序期间存储的重复块.我正处于学习曲线中.
感谢大家阅读
嘿所以我正在创建一个序列化函数,它接受一个基类指针'Joint',提取它'type'的联合后代,然后想要根据指针实际指向的任何类型的'关节' 来实例化正确类型的'定义'. .
然而,我仍然得到关于基类关节不包含后代类确实具有的函数的错误,即使我static_cast指向正确类型的指针.我如何使编译器意识到指针正在被铸造一个具有该功能的新类型?
我也得到关于'typedef'的错误,它可以根据关节*的'type'而不同,编译说它是未定义的.我如何告诉编译器无论如何,其中一个if语句都是真的?(Jointdef在if语句中声明)
这是来源:
template<class Archive>
b2Joint* const preSave(Archive & ar, b2Joint* Joint)
{
int Type = Joint->GetType();
ar & Type; // pulled out first so we know what kind of JointDef to instantiate
if(Type == b2JointType::e_distanceJoint){
b2DistanceJointDef JointDef;
static_cast<b2DistanceJoint *>(Joint);
JointDef.localAnchorA= Joint->GetAnchorA();
JointDef.localAnchorB= Joint->GetAnchorB();
JointDef.length= Joint->GetLength();
JointDef.frequencyHz= Joint->GetFrequency();
}
if(Type == b2JointType::e_weldJoint){
b2WeldJointDef JointDef;
static_cast<b2WeldJoint *>(Joint);
JointDef.localAnchorA= Joint->GetAnchorA();
JointDef.localAnchorB= Joint->GetAnchorB();
JointDef.referenceAngle= Joint->GetReferenceAngle(); // function added
}
if(Type == …Run Code Online (Sandbox Code Playgroud) pointers ×10
c++ ×7
c ×4
arrays ×2
base-class ×1
declaration ×1
function ×1
if-statement ×1
int ×1
join ×1
reference ×1
static-cast ×1
stl ×1