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 6693989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:03:00+00:00 2026-05-26T06:03:00+00:00

I have this code: #include <iostream> class Super{ public: virtual void showName(); }; class

  • 0

I have this code:

#include <iostream>

class Super{
public:
    virtual void showName();
};

class Special1 : public Super {
public:
    void showName();
    void sayHello();
};

class Special2 : public Super {
public:
    void showName();
    void sayGoodbye();
};

void Super::showName() {
    std::cout << "I'm super!" << std::endl;
}

void Special1::showName() {
    std::cout << "I'm special1" << std::endl;
}

void Special1::sayHello() {
    std::cout << "Hello" << std::endl;
}

void Special2::showName() {
    std::cout << "I'm special2" << std::endl;
}

void Special2::sayGoodbye() {
    std::cout << "Goodbye" << std::endl;
}

int main () {
    Super *oSpec=new Super;

    Special1 *o1=static_cast<Special1 *>(oSpec);
    Special2 *o2=static_cast<Special2 *>(oSpec);

    oSpec->showName();
    o1->showName();
    o2->showName();

    o1->sayHello();
    o2->sayGoodbye();

    delete oSpec;

    return 0;
}

When I run it, it shows this output:

I'm super!
I'm super!
I'm super!
Hello
Goodbye

But, if I remove the virtual keyword from the declaration of the Super class:

class Super{
public:
    /*virtual*/ void showName();
};

The output becomes the correct one:

I'm super!
I'm special1
I'm special2
Hello
Goodbye

Then, my question is, why the presence of the virtual keyword makes the pointers o1 and o2 run the method Super::showName() instead of Special1::showName() or Special2::showName()?

  • 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-26T06:03:00+00:00Added an answer on May 26, 2026 at 6:03 am

    The output is closer to correct with virtual and incorrect with it (unless you really wanted that). The cast doesn’t change the type of the objects, just the type of the pointers. They are still of type Super, and so it’s Super::showName that should run.

    Casting one pointer type to another pointer type doesn’t change the type of the thing pointed to. How could it? The whole point of virtual functions is to be able to call methods on ‘generic’ pointers and get the correct derived class method.

    The classic example of why you use virtual functions is for musicians. You might have a function that causes the entire orchestra to play by calling the Play method on every Musician * it gets passed. For a Pianist, that has to call Pianist::Play.

    Normally, the compiler figures out which function to call at compile time — early binding. The only information the compiler is sure to know is the type of the pointer. The virtual keyword causes the binding to occur late, at run time, when the actual type of the class member is known.

    By the way, you can still call the base class method, by using a scope override. For example o1->Super::showName();.

    In fact, the result you claim is ‘correct’ is catastrophic. Running Special1::showName() with the this pointer pointing to an object that is not of type Special1 (or something derived from it) is undefined behavior and can easily lead to a crash.

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

Sidebar

Related Questions

I have the following code: #include <iostream> class Grandma { public: virtual void foo()
I have this code #include <iostream> using namespace std; class Test{ public: int a;
I have this code: #include <iostream> class foo { public: foo(int yy){y = yy;}
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Suppose I have this code: class A { }; class B: virtual public A
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
I have this code from programming pearls #include <iostream> //#include <string> using namespace std;
I have this snippet of the code account.cpp #include account.h #include <iostream> #include <string>
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int

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.