Wednesday, 15 August 2012

inheritance - In C++, does a constructor that takes the base class count as a copy constructor? -



inheritance - In C++, does a constructor that takes the base class count as a copy constructor? -

for example:

class derived : public base of operations { derived(const base of operations &rhs) { // re-create constructor? } const derived &operator=(const base of operations &rhs) { // re-create assignment operator? } }; does constructor shown count re-create constructor? does assignment operator shown count re-create assignment operator?

does constructor shown count re-create constructor?

no. not count copy constructor. conversion constructor not re-create constructor.

c++03 standard copying class objects para 2:

a non-template constructor class x re-create constructor if first parameter of type x&, const x&, volatile x& or const volatile x&, , either there no other parameters or else other parameters have default arguments.

does assignment operator shown count re-create assignment operator?

no, doesn't.

c++03 standard 12.8 copying class objects para 9:

a user-declared re-create assignment operator x::operator= non-static non-template fellow member function of class x 1 parameter of type x, x&, const x&, volatile x& or const volatile x&.

online sample:

#include<iostream> class base{}; class derived : public base of operations { public: derived(){} derived(const base of operations &rhs) { std::cout<<"\n in conversion constructor"; } const derived &operator=(const base of operations &rhs) { std::cout<<"\n in operator="; homecoming *this; } }; void dosomething(derived obj) { std::cout<<"\n in dosomething"; } int main() { base of operations obj1; dosomething(obj1); derived obj2; obj2 = obj1; homecoming 0; }

output:

in conversion constructor in dosomething in operator=

c++ inheritance copy-constructor copy-assignment

No comments:

Post a Comment