For example:
class Derived : public Base
{
Derived(const Base &rhs)
{
// Is this a copy constructor?
}
const Derived &operator=(const Base &rhs)
{
// Is this a copy assignment operator?
}
};
Does the constructor shown count as a copy constructor?
No. It does not count as a copy constructor.
It is just a conversion constructor not a copy constructor.
C++03 Standard Copying class objects Para 2:
A non-template constructor for class
X
is a copy constructor if its first parameter is of typeX&
,const X&
,volatile X&
orconst volatile X&
, and either there are no other parameters or else all other parameters have default arguments.
Does the assignment operator shown count as a copy assignment operator?
No, it doesn't.
C++03 Standard 12.8 Copying class objects Para 9:
A user-declared copy assignment operator
X::operator=
is a non-static non-template member function of classX
with exactly one parameter of typeX
,X&
,const X&
,volatile X&
orconst volatile X&
.
Online Sample:
#include<iostream>
class Base{};
class Derived : public Base
{
public:
Derived(){}
Derived(const Base &rhs)
{
std::cout<<"\n In conversion constructor";
}
const Derived &operator=(const Base &rhs)
{
std::cout<<"\n In operator=";
return *this;
}
};
void doSomething(Derived obj)
{
std::cout<<"\n In doSomething";
}
int main()
{
Base obj1;
doSomething(obj1);
Derived obj2;
obj2 = obj1;
return 0;
}
Output:
In conversion constructor
In doSomething
In operator=
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