• 0

[C++] Virtual


Question

Hey, why is it that the destructor of a class (that inherits something from a base class and the use of virtual is appropriate) is virtual but the constructor is not? Besides the point of the compiler not letting you anyway and that they tell you it's wrong!

Also, a copy constructor passes values in by reference right? I was wondering what would happen if it was by value instead (obviously this is wrong). But how come? I figured by passing in by reference, it would copy the actual object at that address. If you pass in by value, wouldn't it have to make a copy first? (using the copy constructor?!?!) Huh!? :huh:

Edited by fault
Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Virtual ensures that you implement it in derived classes and ensures that the correct function is called. Obviously, you always implement a constructor of some sort, or the compiler provides a default one. At least, that is how I understand it.

Your correct on your last... um, questions? lol :p

Link to comment
Share on other sites

  • 0

Here's a simple answer, the derived class's constructor is the one always called.

I don't believe C++ has the super keywords though, so you are unable to reuse code from a parent constructor afaik.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.