我想知道如何在C++的构造函数中创建一个新创建的对象的指针?在构造函数中创建一个对象的指针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);
}
非常感谢,正是我一直在寻找! – Christiaan 2013-03-18 21:11:33
@Christiaan:很高兴帮助。如果这可以解决您的问题,请考虑将答案标记为已接受(或任何其他正确答案):) – 2013-03-18 21:12:36