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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:46:17+00:00 2026-06-04T11:46:17+00:00

I have the header file testcode.h #ifndef TESTCODE_H #define TESTCODE_H class A { public:

  • 0

I have the header file “testcode.h”

#ifndef TESTCODE_H
#define TESTCODE_H

class A
{
public:
    A();
    ~A();
    virtual void Foo();

public:
    int mPublic;

protected:
    int mProtected;

private:
    int mPrivate;
};

class B : public A
{
public:
    B();
    ~B();
    void Foo();
};

#endif // TESTCODE_H

and a source file

#include "TestCode.h"

int main(int argc, char* argv[])
{
    A* b = new B();
    b->Foo();

    b->mPublic = 0;
    b->mProtected = 0;
    b->mPrivate = 0;

    delete b;

    return 0;
}

Here, i would like to know that when I am calling “b->Foo”, the Foo function of the class B is called instead of class A. However, the Foo function of class B is not declared as virtual. Can anyone elaborate on this ??

  • 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-04T11:46:18+00:00Added an answer on June 4, 2026 at 11:46 am

    Once a function is declared virtual in a base class, it doesn’t matter if the virtual keyword is used in the derived class’s function. It will always be virtual in derived classes (whether or not it is so declared).

    From the C++11 standard, 10.3.2:

    If a virtual member function vf is declared in a class Base and in a
    class Derived, derived directly or indirectly from Base, a member
    function vf with the same name, parameter-type-list (8.3.5),
    cv-qualification, and refqualifier (or absence of same) as Base::vf is
    declared, then Derived::vf is also virtual (whether or not it is so
    declared) and it overrides Base::vf. …

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

Sidebar

Related Questions

I have a header file: class day { public: day(int id); ~day(); private: int
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
I have a header file with all the enums listed (#ifndef #define #endif construct
I have a header file: class A { public: DeviceProxyPtr GetDeviceProxy(); }; DeviceProxyPtr is
I have a header file: using namespace std; class IntList{ private: int *Intl; int
I ran into such problem: I have one header file: //first variant: #ifndef LIBRARIES_H
Let's say I have a header file with a class that uses std::string .
I have a header file that contains a class. Within that class, I have
I have a header file including a structure like this: typedef struct { int
I have a header file with some inline template methods. I added a class

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.