我想知道如何在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