Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:36+00:00 2026-05-25T16:22:36+00:00

C++ type change during deletion I’ve read that when you construct a derived type,

  • 0

C++ type change during deletion

I’ve read that when you construct a derived type, the type changes depending on which constructor is being called. So, if you create a derived object and call a virtual function using a base pointer, normally it would map to the implementation in the derived class. If you called the virtual function in the base class constructor though, it would use the base class implementation as the type of the object is technically that of the base class while in that function. For example (makeshift code, sorry if it doesn’t compile):

class Base { 
    Base()
    {
        std::cerr << "Base Constructor.";
        func();
    }

    virtual void func() {
        std::cerr << "Func base called." << std::endl;
    }
};

class Derived : public Base {
    Derived()
    {
        std::cerr << "Derived Constructor.";
        func();
    }

    void func() {
        std::cerr << "Func derived called." << std::endl;
    }
};

int main() {
    Derived* d = new Derived;
    delete d;
}

Should output:

Base Constructor.
Func base called.
Derived Constructor.
Func derived called.

First of all, is this always true or is it implementation dependent?

If I used RTTI and typeinfo, would the type printed in the base actually be that of the base, or is this more of an unwritten rule sort of situation?

Is it dangerous to call virtual functions from constructors with this in mind, or is it safe as long as you know what you’re doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:22:37+00:00Added an answer on May 25, 2026 at 4:22 pm

    To keep it short and simple, you can have a Rule:

    The virtual mechanism is disabled in Constructors and Destructors

    A virtual function call in Base class will always call the base class version of the function, the same in derived class results in call to the Derived class version of the function.

    First of all, is this always true or is it implementation dependent?

    Yes this is always true. This is not implementation-dependent.

    If I used RTTI and typeinfo, would the type printed in the base actually be that of the base?

    Yes it would be of Base; Derived object doesn’t even exist while you are in Base class constructor.

    Is it dangerous to call virtual functions from constructors with this in mind, or is it safe as long as you know what you’re doing?

    No it is not dangerous to call virtual functions from constructor as long as you understand the semantics behind it.


    This C++ FAQ should be a good read for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a type with about 40 properties (all value types) that represents a
I want to change the default error messages being thrown by Spring. I basically
I was looking for a way to change an Image during a storyboard, or
Here is the exception: Serious application error. Exception was caught during Core Data change
I need a way to verify during compile time that upcast/downcast of a pointer
(Follow-Up-Question to How to change LINQ O/R-M table name/source during runtime? ) I need
I've got a project called DotRas on CodePlex that exposes a component called RasConnectionWatcher
Using the Entity Framework that I generated. I have a Roles table created during
Hi I am trying to change the type of <input> from submit to image
Is it possible to change a methods signature in Java depending on the parameter?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.