Question

I have a class Base and Class derived .

  1. If i declare a copy constructor in my class, will the compiler define the copy constructor while compiling?
  2. What will happen if the Derived class copy constructor is called ? Is it depend on the Base class copy constructor ?

(I need to know the relation between base class and derived in terms of copy constructor during standard compilation. what does and how does the compiler define the copy constructor)

Était-ce utile?

La solution

If i declare a copy constructor in my class, will the compiler define the copy constructor while compiling?

No. It will not.

If you just declare the copy constructor and don't define it, You will end up with a linking error. By providing a explicit declaration for the copy constructor you tell the compiler that the implicitly generated one is not enough for your needs and make a promise that you will provide your own version. But when you don't provide the definition you break that promise.

What will happen if the Derived class copy constructor is called ? Is it depend on the Base class copy constructor?

The presence or absence of copy constructor in Base class does not affect the copy constructor in derived class. The rules are simple:
Compiler generates a copy constructor for your class if you do not provide an explicit declaration. Note that the compiler does so only if your code uses the copy constructor. If the compiler detects that your code never uses the copy constructor then it will not generate a copy constructor simply because it is an unrequired overhead and breaks the fundamental rule of C++, "You pay only for what you use"

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top