-2

如何使指针指向同一类的真实对象的内存位置?

Class A
{
    int num = 0;
    A(int n) {
        this->num = n;
    }
}

int main()
{
    A a(10);
    A * aPtr;

    // How do I make "aPtr" point at "a"?

    // something like
    // aPtr = a; (I know this wont work but what instead)
}
4

2 回答 2

1

如何使“aPtr”指向“a”?

使用&(address-of) 运算符:

aPtr = &a;
于 2013-03-14T23:36:10.850 回答
1

使用地址运算符:

A * aPtr = &a;                 // assume that & is not overloaded

#include <memory>

A * aPtr = std::addressof(a);  // more general
于 2013-03-14T23:36:23.500 回答