Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ create default "Constructor/Destructor/Copy Constructor/Copy assignment operator" for pure virtual class?

Do C++ compilers generate the default functions like Constructor/Destructor/Copy-Constructor... for this "class"?

class IMyInterface
{
    virtual void MyInterfaceFunction() = 0;
}

I mean it is not possible to instantiate this "class", so i think no default functions are generated. Otherwise, people are saying you have to use a virtual destructor. Which means if i dont define the destructor virtual it will be default created, not virtual.

Furthermore i wannt to know if it is reasonable to define a virtual destructor for a pure virtual Interface, like the one above? (So no pointers or data is used in here, so nothing has to be destructed)

Thanks.

like image 613
user1911091 Avatar asked Jan 23 '14 11:01

user1911091


People also ask

Are copy constructors default?

If we don't define our own copy constructor, the C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects. The compiler-created copy constructor works fine in general.

Can a constructor be pure virtual?

Virtual Constructor in C++ In C++, the constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet. So, the constructor should always be non-virtual. But virtual destructor is possible.

Is there a default copy constructor in C++?

Default Copy Constructors: When a copy constructor is not defined, the C++ compiler automatically supplies with its self-generated constructor that copies the values of the object to the new object.

What is default destructor C?

The default destructor calls the destructors of the base class and members of the derived class. The destructors of base classes and members are called in the reverse order of the completion of their constructor: The destructor for a class object is called before destructors for members and bases are called.


2 Answers

Yes.

There is no wording that requires the class to be instantiable in order for these special member functions to be implicitly declared.

This makes sense — just because you cannot instantiate the Base, doesn't mean a Derived class doesn't want to use these functions.

struct Base
{
   virtual void foo() = 0;
   int x;
};

struct Derived : Base
{
   Derived() {};         // needs access to Base's trivial implicit ctor
   virtual void foo() {}
};

See:

  • §12.1/5 (ctor)
  • §12.8/9 (move)
  • §12.8/20 (copy)
like image 136
Lightness Races in Orbit Avatar answered Sep 18 '22 20:09

Lightness Races in Orbit


Furthermore i wannt to know if it is reasonable to define a virtual destructor for a pure virtual Interface, like the one above? (So no pointers or data is used in here, so nothing has to be destructed)

It's not only reasonable, it's recommended. This is because in the case of virtual function hierarchies, (automatically) calling a destructor of a specialized class also calls all destructors of it's base classes. If they are not defined, you should get a linking error.

If you define at least one virtual function in your class you should also define a virtual destructor.

The destructor can be defined with =default though:

Here's a corrected (compilable) code example:

class ImyInterface
{
    virtual void myInterfaceFunction() = 0;
    virtual ~ImyInterface() = 0;
}

ImyInterface::~ImyInterface() = default;
like image 34
utnapistim Avatar answered Sep 21 '22 20:09

utnapistim