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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:13:10+00:00 2026-05-23T05:13:10+00:00

What does the keyword virtual do when overriding a method? I’m not using it

  • 0

What does the keyword virtual do when overriding a method? I’m not using it and everything works fine.

Does every compiler behave the same in this regard?

Should I use it or not?

  • 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-23T05:13:11+00:00Added an answer on May 23, 2026 at 5:13 am

    You cannot override a member function without it.

    You can only hide one.

    struct Base {
       void foo() {}
    };
    
    struct Derived : Base {
       void foo() {}
    };
    

    Derived::foo does not override Base::foo; it simply hides it because it has the same name, such that the following:

    Derived d;
    d.foo();
    

    invokes Derived::foo.

    virtual enables polymorphism such that you actually override functions:

    struct Base {
       virtual void foo() {}
    };
    
    struct Derived : Base {
       virtual void foo() {} // * second `virtual` is optional, but clearest
    };
    
    Derived d;
    Base& b = d;
    b.foo();
    

    This invokes Derived::foo, because this now overrides Base::foo — your object is polymorphic.

    (You also have to use references or pointers for this, due to the slicing problem.)


    • Derived::foo doesn’t need to repeat the virtual keyword because Base::foo has already used it. This is guaranteed by the standard, and you can rely on it. However, some think it best to keep that in for clarity.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says really. Does the virtual keyword cause a performance hit?
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
I am new to Oracle. Is there a builtin keyword does the same job
What does 'this' keyword refer to when used in global object? Let's say for
What does the volatile keyword do? In C++ what problem does it solve? In
What does the explicit keyword mean in C++?
What does the notwithstanding keyword mean in Google Go?
What functionality does the yield keyword in Python provide? For example, I'm trying to
Does filling out HTML meta description/keyword tags matter for SEO?
What does the SQL Action keyword do? Can I use this keyword in a

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.