Possible Duplicate:
How to initialize a const field in constructor?
I have this class:
class Foo {
private:
...
public:
Foo() : ... {}
// no other constructors
...
};
and another one which holds a Foo member by reference:
class Bar {
private:
const Foo& m_foo;
...
public:
Bar(const Foo& foo);
// no other constructors
};
My question is: how do i initialize the Bar::m_foo reference at the constructor?
Thanks!
In the constructor initialization list:
Bar(const Foo& foo) : m_foo(foo)
{
}
const and reference members must be initialized in the initialization list, in this case the member is both.
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