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 reference-type variable that is readonly , because the reference never change,
If I change the type to const char str[Len] , I get the following
I need to change the type of my primary key column on a table
Is it possible to change the type of a TFS work item? For example,
If I wish to simply rename a column (not change its type or constraints,
Is there anyway to change the content-type of an XML document, in the XML
Why does Resharper want you to change most variables to var type instead of
How to change at runtime by code (not for groupskintype), the skin type for
If you change an anything (entity, relationship, type of attribute etc) in a CoreData
i am using below code to change the the font type of text view.

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.