Given the following classes where ConcreteBar
implements BarIfc
:
class Base {
public:
Base(BarIfc& bar) : _bar(bar) { }
void foo() {
_bar.bar();
}
private:
Base(const Base& other);
Base& operator=(const Base& other);
BarIfc& _bar; // Pure virtual interface
};
class Child : public Base {
public:
// bar is passed to base class before initialization
Child(int i) : Base(_bar), _bar(i) {
}
private:
Child(const Child& other);
Child& operator=(const Child& other);
ConcreteBar _bar;
};
Am I right in assuming that this
Child(int i) : Base(_bar), _bar(i) {}
is "valid" C++ as long as I'm not using (e.g. calling a method) _bar
reference within the initializer list of the base class?
The base keyword can be used with or without parameters. Any parameters to the constructor can be used as parameters to base , or as part of an expression.
To pass arguments to a constructor in a base class, use an expanded form of the derived class' constructor declaration, which passes arguments along to one or more base class constructors. Here, base1 through baseN are the names of the base classes inherited by the derived class.
If we want to pass arguments to the base class's constructor from the constructor of the child class, we have to use the base keyword in C#. The base keyword specifies which constructor of the base class should be called when an instance of the child class is created.
In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class.
Under the assumption that ConcreteBar is a subobject
It is valid since the storage has been allocated for §3.7.5/1
The storage duration of member subobjects, base class subobjects and array elements is that of their complete object (1.8).
and §3.8/5 says:
Before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any pointer that refers to the storage location where the object will be or was located may be used but only in limited ways. For an object under construction or destruction, see 12.7 [referring to non-static member usage]
so that is valid as long as you don't use the reference.
Yes it's valid, because while _bar
hasn't been constructed yet, the storage for it does exist, and taking a reference to it is OK so long as you don't actually use that reference until you're in the derived class constructor body.
Here's another post on a similar topic that you may find illuminating: https://stackoverflow.com/a/5905746/4323
And finally, an answer which quotes the standard: https://stackoverflow.com/a/6258431/4323 saying:
Before the lifetime of an object has started but after the storage which the object will occupy has been allocated [...], any pointer that refers to the storage location where the object will be or was located may be used but only in limited ways.
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