Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing class member to base class constructor (by reference)

Tags:

c++

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?

like image 216
Sambuca Avatar asked Oct 14 '14 07:10

Sambuca


People also ask

Can we pass parameters to base class constructor?

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.

Can we pass parameters to base class constructor though derived class or?

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.

How do you pass value from the constructor of a child class to the constructor of a base class in C#?

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.

Can constructor be inherited from base class?

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.


2 Answers

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.

like image 114
Marco A. Avatar answered Sep 19 '22 05:09

Marco A.


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.

like image 29
John Zwinck Avatar answered Sep 19 '22 05:09

John Zwinck