Calling base's move constructor from derived copy constructor - c++

I'm new to SO so let me know if I need to change anything. I did my best to be as thorough and provide example code. I know that many similar questions have been asked, I was unable to find one matching my specific problem though.
Furthermore, I'm aware that what I'm doing is not something one would do in 'real' code, I'm just trying to get a better understanding of r/l/p/x..values.
I have a base and a derived class, both having the default, copy, and move constructors. Now I want to have the copy constructor of the derived class calling the move constructor of the base class.
class Base
{
public:
Base(){ std::cout << "default base constructor called.\n"; }
Base(Base const &other) { std::cout << "copy base constructor called.\n"; }
Base(Base &&tmp) { std::cout << "move base constructor called.\n"; }
};
And basically the same for the derived class:
class Derived : public Base
{
public:
Derived(){ std::cout << "default derived constructor called.\n";}
Derived(Derived const &other)
:
Base(std::move(other)) // here I want to call Base(Base &&tmp)
{
std::cout << "copy derived constructor called.\n";
}
Derived(Derived &&tmp)
:
Base(std::move(tmp)) // correctly calls Base(Base &&tmp)!
{
std::cout << "move derived constructor called.\n";
}
};
So in my main function, I now want to call the copy constructor, which then calls the move constructor of the base class.
int main()
{
Derived der{};
Derived der_copy{ der };
Derived der_move{ std::move(der) };
}
The output I would get is this:
default base constructor called.
default derived constructor called.
copy base constructor called. <-- why not move?
copy derived constructor called.
move base constructor called.
move derived constructor called.
I was expecting the following:
default base constructor called.
default derived constructor called.
move base constructor called.
copy derived constructor called.
move base constructor called.
move derived constructor called.
So when I use std::move(tmp) in the derived move constructor (so on Base &&tmp) that the base move constructor is called, but when I use std::move(other) in the derived copy constructor (so on Base const &other) that the base copy constructor is called?
Tbh, this seems so strange that I'm afraid that I just made a mistake in my code, I checked everything multiple times but I can't seem to get the move base constructor called in the case above...
Thanks for your help!

In the copy constructor
Derived(const Derived& other)
std::move(other) will result in an xvalue expression of type const Derived&&.
This is a legal but somewhat weird type: std::move(other) is a temporary object, but you can't move from it, because it is constant. Such references have a limited number of use cases. See the declarations of std::as_const and std::ref for one particular example.
const Derived&& cannot bind to Base&&, that's why during the overload resolution between
Base(const Base&)
Base(Base&&)
the former is chosen by the compiler.
At the risk of getting undefined behaviour, you can cast constness away and write
Derived(const Derived& other) : Base(std::move(const_cast<Derived&>(other))) {}
to call the move constructor of Base. But don't do it in real code.

You need to change your Base class like this:
Base(const Base &&tmp) { std::cout << "move base constructor called.\n"; }

Related

Why is user defined copy constructor calling base constructor while default copy constructor doesn't? [duplicate]

This question already has answers here:
Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn't?
(3 answers)
Closed 11 months ago.
Consider the following example:
class A
{
public:
A()
{
cout<<"constructor A called: "<<this<<endl;
};
A(A const& other) = default;
};
class B : public A
{
public:
B()
{
cout<<"constructor B called: "<<this<<endl;
};
//This calls A's constructor
B(B const& other)
{
cout<<"B copy constructor called: "<<this<<endl;
};
//While this doesn't call A's constructor
//B(B const& other) = default;
};
int main()
{
B b;
B b2(b);
cout<<"b: "<<&b<<endl;
cout<<"b2: "<<&b2<<endl;
return 0;
}
Output:
constructor A called: 0x7fffc2fddda8
constructor B called: 0x7fffc2fddda8
constructor A called: 0x7fffc2fdddb0
B copy constructor called: 0x7fffc2fdddb0
b: 0x7fffc2fddda8
b2: 0x7fffc2fdddb0
Why is the constructor of A is called when copying B?
Shouldn't the copy constructor of A be called instead?
However, if you change class B's copy constructor to be default, the constructor of A is not called when copying which makes sense.
It will be nice if someone can give a reasonable explanation as to why.
Why is the constructor of A is called when copying B? Shouldn't the copy constructor of A be called instead?
No, it shouldn't.
A derived class must always initialize a base class. If the derived class has a constructor that is implemented explicitly by the user, but it does not explicitly call a base class constructor in its member initialization list, the compiler will make an implicit call to the base class's default constructor, regardless of the type of the derived constructor. The compiler does not make any assumption about the user's intent in implementing the derived constructor. If the user wants a specific base class constructor to be called, they need to make that call themselves.
Since B has an explicitly implemented copy constructor that lacks a member initialization list, the compiler initializes A by calling its default constructor, not its copy constructor.
IOW, this:
B(B const& other)
{
...
}
Is equivalent to this:
B(B const& other) : A()
{
...
}
NOT to this, as you are thinking:
B(B const& other) : A(other)
{
...
}
However, if you change class B's copy constructor to be default, the constructor of A is not called when copying which makes sense.
Correct, a default'ed copy constructor will call the base class's copy constructor, not its default constructor. The compiler is implicitly implementing the entire derived constructor, and so it will choose the appropriate base class constructor to call.

Why are copy constructors of base classes not implicitly called? [duplicate]

This question already has answers here:
Why aren't copy constructors "chained" like default constructors and destructors?
(3 answers)
Closed 3 years ago.
From my understanding, when creating an object of a derived class the base class constructor get's automatically called (in case a one without parameters exists). This seems to not be the case for copy constructors:
#include <iostream>
class Base
{
public:
Base()
{
std::cout << "Base Constructor called" << std::endl;
}
Base(const Base& ref)
{
std::cout << "Base Copy Constructor called" << std::endl;
}
};
class Derived : Base
{
public:
Derived()
{
std::cout << "Derived Constructor called" << std::endl;
}
Derived(const Derived& ref) //: Base(ref) // <- without this Base copy constructor doesnt get called
{
std::cout << "Derived Copy Constructor called" << std::endl;
}
};
int main()
{
Derived d1;
Derived d2 = d1;
return 0;
}
Output without ": Base(ref)" :
Base Constructor called
Derived Constructor called
Base Constructor called
Derived Copy Constructor called
Output with ": Base(ref)" :
Base Constructor called
Derived Constructor called
Base Copy Constructor called
Derived Copy Constructor called
So unless you explicitly call the Base class copy constructor a new Base class object gets created instead of being copy constructed. So i guess only the Derived class members get actually copy constructed while all Base class members would be newly created by the Base class constructor. Now for me this seems like something you never really want. If you copy construct an object you expect all members to be copied instead of just a part of them.
Why are copy constructors different from normal constructors in this regard?
Why are copy constructors different from normal constructors in this regard?
They aren't. When you do not call a base class constructor, the default constructor is called for you. This happens for every contructor.

What's not inherited to a C++ Derived class? Apparently, operator= and some constructors are actually inherited

I am trying to learn about C++ inheritance, but one thing doesn't make any sense to me.
Everything a googled about what is not inherited by a derived class said that the constructors, friends, and operator= are not inherited. However, this information doesn't fit with the results of my program.
I did an example of inheritance and the result is what follows:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "constructor base class without parameters" << endl;
}
Base(int a)
{
cout << "constructor base class with int parameter" << endl;
}
Base(const Base& b)
{
cout << "copy constructor base class" << endl;
}
Base& operator= (const Base& base)
{
cout << "operator= base class" << endl;
}
};
class Derived: public Base
{
};
int main()
{
Derived d;
cout << endl << "here 1" << endl << endl;
Derived d2 = d;
cout << endl << "here 2" << endl << endl;
d = d2;
//Derived d3 (3); // ERROR!!
}
The output was:
constructor base class without parameters
here 1
copy constructor base class
here 2
operator= base class
If all the constructors and operator= are not inherited, why were operator=, default constructor and copy constructor of the base class called?
Dervied has no constructors, in this case a default constructor is generated which calls the default constructor of all base classes and members.
Similar things happen with the copy constructor and assignment operator. The Base class versions are being called by automatically generated Derived class versions.
This has nothing to do with inheritance of constructors or assignment operators.
Classes don't automatically inherit constructors, although you can force them to provide a base class's constructors with a using statement:
class Derived: public Base
{
public:
// Force this class to provide the base class's constructors
using Base::Base;
// Force this class to provide the base class's assignment operator
using Base::operator=;
};
The copy constructor wasn't inherited either. Instead, the compiler automatically generated a copy constructor for the derived class. If all of a class's member variables and base classes are copyable, then the class itself is copyable, and it'll generate the copy constructor automatically.
The same rules apply to the assignment operator: if all of a class's members provide a copy assignment operator, the compiler automatically generates one for the class itself.
Copy/move constructor is the exception according to the standard.
Referring to the standard:
For each non-template constructor in the candidate set of inherited
constructors other than a constructor having no parameters or a
copy/move constructor having a single parameter, a constructor is
implicitly declared with the same constructor characteristics unless
there is a user-declared constructor with the same signature in the
complete class where the using-declaration appears or the constructor
would be a default, copy, or move constructor for that class.
PS. Base& operator = (const Base& base)is not a constructor. It is assignment. So it works like any other member function. Since it is public in your example, it got inherited.

how to force base class constructors to be called in derived classes?

basic c++ question i'm fairly sure. if i have a base class with a constructor that takes no parameters, and just initializes some of the protected members, does a derived class instantly call this base constructor too if it matches the parameters (wishful but unlikely thinking), and if not, is there a way to force it to automatically call said base constructor from the derived class WITHOUT having to explicitly tell it to do so in the derived class? I ask because i'm writing a wrapper of sorts and there are some protected members that i want initialized to specific values initially, and then i want to derive and manipulate this base class to my needs, but i wouldn't like an outside user to have to remember to explicitly call the base constructor or set these values within their own constructor.
Yes, the default base constructor is always called unless explicitly stated otherwise.
For example:
class A
{
public:
A() { std::cout << "A"; }
};
class B : A
{
public:
B() {}
};
int main()
{
B b;
return 0;
}
will output:
A
By "explicitly stated otherwise" I mean that you can call a different constructor from the derived class:
class A
{
public:
A() { std::cout << "A"; }
A(int) { std::cout << "AAA"; }
};
class B : A
{
public:
B() : A(1) {} //call A(int)
};
int main()
{
B b;
return 0;
}
will output
AAA
Important if you don't have a default constructor (you declare a non-default constructor and not a default one) or the default constructor is not visible (marked as private), you need to explicitly call an available constructor in the derived class.
If your base-class has a "default constructor" (a constructor that takes no parameters; either explicitly provided by you, or implicitly provided by the compiler because you didn't explicitly provide any constructors), then every derived-class constructor will automatically call that unless you specify that they call a different constructor instead.
(If your base-class doesn't have a "default constructor", because you've provided one or more constructors that take parameters and no constructor that doesn't, then it's a compile-error for a derived-class constructor not to indicate the base-class constructor it calls.)

Object oriented programming , inheritance , copy constructors

Suppose I have a base class Person and I publicly inherit a class Teacher from base class Person.
Now in the main function I write something like this
// name will be passed to the base class constructor and 17
// is for derived class constructor.
Teacher object(“name”,17) ;
Teacher object1=object; //call to copy constructor
Now I have not written the copy constructor for both the classes, off course the default copy constructors will be called. The Person class’s default copy constructor will first call the base class’s copy constructor.
Now the problem is suppose I write the copy constructor for the base class only, what happens is, the default copy constructor of the derived class will call my written copy constructor.
Now suppose I write the copy constructor for both the classes . now the copy constructor of the derived class (i.e Teacher) will call the default constructor of the base class but not the copy constructor why?
Is only default copy constructor of the derived class can call the copy constructor of the base class automatically?
You have to call the base copy constructor explicitly:
Teacher(const Teacher& other)
: Person(other) // <--- call Person's copy constructor.
, num_(other.num_)
{
}
Otherwise Person's default constructor will be called.
I seem to not fully understand the question so I'll just say everything I think is relevant and hopefully this will help the OP.
All user defined constructors call their base's default constructor by default (unless they explicitly call a different constructor), it doesn't matter if the base's default constructor is user defined or compiler generated.
When a copy constructor is generated by the compiler it will call the base class's copy constructor.
Compiler defined constructors are not special, they can be called explicitly:
class Base {
int num_
public:
Base(int n) : num_(n) { }
// copy constructor defined by compiler
};
class Derived : public Base {
float flt_;
public:
Derived(float f, int n) : Base(n), flt_(f) { }
// Copy constructor
Derived(const Derived& other)
: Base(other) // OK to explicitly call compiler generated copy constructor
, flt_(other.flt_)
{
}
};
For more details see this Wikipedia article.
If you don't specify a copy constructor the compiler generates one automatically. This constructor is generated in a way that it calls the copy constructor of the base class.
If you implement the copy constructor yourself, you also specify what base class constructor should be used (see Motti's answer). If you don't specify anything, the default constructor is used (That's why it is called "default constructor": it is used when no constructor is explicitly specified).
SO the compiler automatically generates a reasonable copy constructor, but if you want something special no further magic is going on and you have to specify yourself how that constructor exactly should look like.
class Base {
int num_
public:
Base(int n) : num_(n) { }
// copy constructor defined by compiler
};
class Derived : public Base {
float flt_;
public:
Derived(float f, int n) : Base(n), flt_(f) { }
// Copy constructor
Derived(const Derived& other)
: Base(other) // OK to explicitly call compiler generated copy constructor
, flt_(other.flt_)
{
}
};