• entries
    626
  • comments
    1446
  • views
    1008209


3 Comments


Recommended Comments

I know your probally whining for a reason but thats one of the few things where I think c++ took the logical choice, if you could call virtual functions from in the constructor then you might call a virtual function from a base class which acesses the members of a derived class before they are initialized so its "cant call them" or "potentially undefined behaviour if you call them", I know which I prefer ;).

Share this comment


Link to comment
Of course it's logical. The only way to get around it would be to force the compilers to basically do two-phase construction transparently (i.e. init the vtable first and then call the normal constructors) which introduces all kinds of nasty problems of its own, and violates the well-known C++ design policy of "you don't pay for what you don't use."

The problem is it ruins a few designs that would otherwise be very elegant, especially when it comes to building classes in the RAII style. Of course, one could make a very good argument that anyone looking for ease of elegant design should steer well clear of C++ in the first place.

Share this comment


Link to comment
ok then, I guess if you really need it you could get the same effect by passing a member function pointer to the class constructor and hide it from users by setting it to a default value, its a bit of extra work since you have to pass the new pointer to the base class in each derived class constructor but its better then nothing.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now