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

  • Home
  • SEARCH
  • 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 6858903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:13:04+00:00 2026-05-27T02:13:04+00:00

Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question

  • 0

Possible Duplicate:
C++ virtual function from constructor
Calling virtual functions inside constructors

This question was asked in interview .

I guess I had answered the 1st part correctly but not sure about the 2nd part. In fact I have no clue about 2nd part.

  1. What output does the following code generate? Why?
  2. What output does it generate if you make A::Foo() a pure virtual function?

When I tried running same question on my compiler with virtual void foo() = 0; it throws
error “undefined reference to `A::Foo()'”

#include <iostream>

using namespace std;

class A     
{    
public:       
    A()             
    {
        this->Foo();
    }
    virtual void Foo() 
    {
        cout << "A::Foo()" << endl;
    }
};

class B : public A      
{     
public:     
    B()      
    {
        this->Foo();      
    }
    virtual void Foo() 
    {
        cout << "B::Foo()" << endl;
    }
};

int main(int, char**)
{
    B   objectB;
    return 0;
}
  • 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-27T02:13:04+00:00Added an answer on May 27, 2026 at 2:13 am

    When you instantiate a B object, the following happens:

    1. B‘s constructor is called.

    2. First thing, B‘s constructor calls the base constructor A().

    3. Inside A‘s constructor, the function call is dispatched to A::foo(), since this has static and dynamic type A* (nothing else makes sense if you think about it); now the A subobject is complete.

    4. Now B‘s constructor body runs. Here the function call is dispatched to B::foo(). Now the entire B object is complete.

    If A::foo() is pure-virtual, step (3) causes undefined behaviour; cf. 10.6/4 in the standard.

    (In your case possibly manifesting as a linker error, since the compiler optimizes to resolve the call statically, and the symbol A::foo is not found.)

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

Sidebar

Related Questions

Possible Duplicate: class has virtual functions and accessible non-virtual destructor I got this code
Possible Duplicate: Why pure virtual function is initialized by 0? In C++, what does
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
Possible Duplicate: Virtual member call in a constructor In C#, is it safe to
Possible Duplicate: Pure virtual functions may not have an inline definition. Why? I've come
Possible Duplicates: Overriding vs Virtual How am i overriding this C++ inherited member function
Possible Duplicate: How could one implement C++ virtual functions in C In C++ the
Possible Duplicate: Learning C++: polymorphism and slicing This is building off a question I
Possible Duplicate: Why C# implements methods as non-virtual by default? I'm speaking primarily about
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be

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.