I recently saw the below code ( simplified version given below) in my code base and got this doubt:
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()) ?
}
First sentence of standard 8.3.2/4:
There shall be no references to references, no arrays of references, and no pointers to references
(My emphasis)
This doesn't mean you can't take the address of a variable declared as a reference, it just means that there's no separate type for a pointer to a reference.
Yes, it's not possible to take the address of a reference. You always get the address of the referred object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With