Destructor & Constructor Question

K

kasiyil

Hello,

I have a question about constructors and destructors in C++. Assume
that I declared a class that has a constructor and virtual destructor
like below:

class X {

X() { printf ("Hi\n"); }
virtual ~X() { }

}

When I derive a class from X called Y, and do not define any
constructor or destructor;
class Y : public X {
void hi() { printf ("Hiiiiiii\n"); }
}

what is the behaivour of the compiler? Does the compiler generates a
constructor that automatically calls X's constuctor? Does the compiler
generates a virtual destructor automatically? Do I need to implement a
virtual destructor?
 
R

Rolf Magnus

kasiyil said:
class X {

X() { printf ("Hi\n"); }
virtual ~X() { }

}
class Y : public X {
void hi() { printf ("Hiiiiiii\n"); }
}

what is the behaivour of the compiler? Does the compiler generates a
constructor that automatically calls X's constuctor?
Yes.

Does the compiler generates a virtual destructor automatically?
Yes.

Do I need to implement a virtual destructor?

Depends on what Y does. If you need to do something on its destruction, yes.
Otherwise, the compiler-generated one will be fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top