我最近在我的代码库中看到了下面的代码(下面给出的简化版本)并且有疑问:
class B;
class A
{
public:
A():m_A("testA"){}
B& getB()
{
return m_B;
}
B* getBPtr() //== > written to explain the problem clearly
{
return &m_B;
}
private:
B m_B;
};
class B
{
public:
B(const std::string& name):m_Name(name){}
std::string getName() const
{
return m_Name;
}
private:
std::string m_Name;
};
class C
{
public:
void testFunc(B* ptr)
{
}
};
int main()
{
A a;
C c;
c.testFunc(&a.getB()); ===> is it equivalent to c.testFunc(a.getBPtr()) ?
}
Run Code Online (Sandbox Code Playgroud)
标准8.3.2/4的第一句话:
不应引用引用,不引用引用数组,也不引用引用指针
(我的重点)
这并不意味着您不能将声明的变量的地址作为引用,它只是意味着指向引用的指针没有单独的类型.
| 归档时间: |
|
| 查看次数: |
204 次 |
| 最近记录: |