Move constructor for derived class

LibertyPaul

I have 2 classes:

template<typename T>
class base{
    T t;
public:
    base(base &&b): t(std::move(b.t)){}
};

template<typename T, typename T2>
class derived : protected base<T>{
    T2 t2;
public:
    derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){}
};

I move entire d object in the derived move-constructor to initialize base part and d becomes invalid but I still need it to use it's part for t2 initialization

Is it possible to do such a thing?

Serge Ballesta

I would say that your construct is correct except for a little syntax error, you need to qualify base<T> in the initializer list:

derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){}

First, the order of initialization is independant of the order of the initializer list. Draft n4296 says in 12.6.2 Initializing bases and members [class.base.init] § 13

In a non-delegating constructor, initialization proceeds in the following order:
(13.1) — First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
(13.2) — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
(13.3) — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
(13.4) — Finally, the compound-statement of the constructor body is executed.

[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]

We have also §7 or same chapter that says:

The initialization performed by each mem-initializer constitutes a full-expression. Any expression in a mem-initializer is evaluated as part of the full-expression that performs the initialization.

My understanding is that standard says that in the move ctor for the derived class, things happens in that order:

  • move ctor for base class is called
    • in turn it calls move ctor for T effectively constructing t member of target and eventually zeroing t member of source
  • move ctor for T2 object is called - at that moment, the end of the full expression has not been reached, and only t member of source has eventually been destroyed
  • at the end of the full statement, source object is left in an undetermined state and should no longer be used.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Overloaded constructor in derived class

From Dev

Error on constructor in derived class

From Dev

Constructor in base and derived class

From Dev

Base Constructor Call in Derived Class

From Dev

copy constructor of derived QT class

From Dev

Derived Class Constructor Returning Null

From Dev

Constructor of class derived from QCustomPlot

From Dev

calling a constructor from a derived class

From Dev

Use derived class for template constructor

From Dev

Visibility of a base class constructor in a derived template class

From Dev

Inadvertent base class constructor "hiding" in derived class

From Dev

Derived Class Constructor from an Abstract Class

From Dev

Use derived class property in base class constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Default move constructor in a sub class

From Dev

Copy constructor with a parameter as a reference to a derived class

From Dev

Fill collection in a proteced constructor of a derived class

From Dev

Undefined reference to in Constructor with QNetworkAccessManager derived class

From Dev

Braces (without constructor) initialization of a derived class

From Dev

C++ CRTP constructor of derived class

From Dev

constexpr for null-initialized constructor in a derived class

From Dev

Calling overloaded base constructor from derived class

From Dev

Setting base members in derived class constructor

From Dev

Derived class's method without constructor

From Dev

constexpr for null-initialized constructor in a derived class

From Dev

Error when when creating Derived Class constructor

From Dev

Python access derived class attributes in parent constructor

From Dev

Class with templated constructor as well as copy and move constructor

Related Related

  1. 1

    Overloaded constructor in derived class

  2. 2

    Error on constructor in derived class

  3. 3

    Constructor in base and derived class

  4. 4

    Base Constructor Call in Derived Class

  5. 5

    copy constructor of derived QT class

  6. 6

    Derived Class Constructor Returning Null

  7. 7

    Constructor of class derived from QCustomPlot

  8. 8

    calling a constructor from a derived class

  9. 9

    Use derived class for template constructor

  10. 10

    Visibility of a base class constructor in a derived template class

  11. 11

    Inadvertent base class constructor "hiding" in derived class

  12. 12

    Derived Class Constructor from an Abstract Class

  13. 13

    Use derived class property in base class constructor

  14. 14

    Calling base class constructor from derived class constructor

  15. 15

    Calling base class constructor from derived class constructor

  16. 16

    Default move constructor in a sub class

  17. 17

    Copy constructor with a parameter as a reference to a derived class

  18. 18

    Fill collection in a proteced constructor of a derived class

  19. 19

    Undefined reference to in Constructor with QNetworkAccessManager derived class

  20. 20

    Braces (without constructor) initialization of a derived class

  21. 21

    C++ CRTP constructor of derived class

  22. 22

    constexpr for null-initialized constructor in a derived class

  23. 23

    Calling overloaded base constructor from derived class

  24. 24

    Setting base members in derived class constructor

  25. 25

    Derived class's method without constructor

  26. 26

    constexpr for null-initialized constructor in a derived class

  27. 27

    Error when when creating Derived Class constructor

  28. 28

    Python access derived class attributes in parent constructor

  29. 29

    Class with templated constructor as well as copy and move constructor

HotTag

Archive