我想知道如何在 c++ 的构造函数中创建一个指向新创建对象的指针?
班级地址是什么?
class MyClass
{
public:
};
class MyClass2
{
public:
//I need a pointer to the created object
MyClass2 *pObjectName;
//Constructor
MyClass2()
{
pObjectName = &//I have no clue how to get the adress of the (not yet) created object.
}
};
int main()
{
//The way it works
//Makes Object
MyClass *pObject;
MyClass Object;
//pObject points to Object
pObject = &Object;
//Prints adress of Object
printf("%p", pObject);
//The way I would like to see it work
MyClass2 Object2;
//Prints adress of Object
printf("%p", Object2.pObjectName);
}