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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:41:12+00:00 2026-06-05T21:41:12+00:00

Let’s assume this scenario in Visual C++ 2010: #include <iostream> using namespace std; struct

  • 0

Let’s assume this scenario in Visual C++ 2010:

#include <iostream>

using namespace std;

struct Base {
    void Display() {
        cout << "Base: Non-virtual display." << endl;
    };
    virtual void vDisplay() {
        cout << "Base: Virtual display." << endl;
    };
};

struct Derived : Base {
    void Display() {
        cout << "Derived: Non-virtual display." << endl;
    };
    virtual void vDisplay() {
        cout << "Derived: Virtual display." << endl;
    };
};

int main() {
    Base ba;
    Derived de;

    ba.Display();
    ba.vDisplay();
    de.Display();
    de.vDisplay();
};

Theoretically, the output of this little application should be:

Base: Non-virtual display.
Base: Virtual display.
Base: Non-virtual display.
Derived: Virtual display.

because the Display method of the Base class is not a virtual method so the Derived class should not be able to override it. Right?

The problem is that when I run the application, it prints this:

Base: Non-virtual display.
Base: Virtual display.
Derived: Non-virtual display.
Derived: Virtual display.

So either I don’t understand the concept of virtual methods or something strange happens in Visual C++.

What is the explanation?

  • 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-06-05T21:41:14+00:00Added an answer on June 5, 2026 at 9:41 pm

    Yep, you are misunderstanding a little.

    The method of the same name on the derived class will hide the parent method in this case. You would imagine that if this weren’t the case, trying to create a method with the same name as a base class non-virtual method should throw an error. It is allowed and it’s not a problem – and if you call the method directly as you have done it will be called fine.

    But, being non-virtual, C++ method lookup mechanisms that allow for polymorphism won’t be used. So for example if you created an instance of your derived class but called your ‘Display’ method via a pointer to the base class, the base’s method will be called, whereas for ‘vDisplay’ the derived method would be called.

    For example, try adding these lines:

    Base *b = &ba;
    b->Display();
    b->vDisplay();
    b = &de;
    b->Display();
    b->vDisplay();
    

    …and observe the output as expected:

    Base: Non-virtual display.
    Base: Virtual display.
    Base: Non-virtual display.
    Derived: Virtual display.

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

Sidebar

Related Questions

Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's assume I want to deserialize this (I've removed the namespaces to make things
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
let's say im returning a datetime string like this in JS: 8/18/2010 9:35:27 AM
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let assume we have two activities. A - main activity, that is home launcher
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's assume that we are building a high traffic site that will be used

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.