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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:32:37+00:00 2026-06-04T21:32:37+00:00

Can someone explain why the result of the code below would be class B::1

  • 0

Can someone explain why the result of the code below would be “class B::1” ?

Why does the virtual method of derived class uses the default parameter of a base class and not his own? For me this is pretty strange. Thanks in advance!

Code:

#include <iostream>

using namespace std;

class A
{
public:
    virtual void func(int a = 1)
    {
        cout << "class A::" << a;
    }
};

class B : public A
{
public:
    virtual void func(int a = 2)
    {
        cout << "class B::" << a;
    }
};

int main()
{
    A * a = new B;
    a->func();

    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-06-04T21:32:40+00:00Added an answer on June 4, 2026 at 9:32 pm

    Because default arguments are resolved according to the static type of this (ie, the type of the variable itself, like A& in A& a;).

    Modifying your example slightly:

    #include <iostream>
    
    class A
    {
    public:
        virtual void func(int a = 1)
        {
            std::cout << "class A::" << a << "\n";
        }
    };
    
    class B : public A
    {
    public:
        virtual void func(int a = 2)
        {
            std::cout << "class B::" << a << "\n";
        }
    };
    
    void func(A& a) { a.func(); }
    
    int main()
    {
        B b;
        func(b);
        b.func();
    
        return 0;
    }
    

    We observe the following output:

    class B::1
    class B::2
    

    In action at ideone.

    It is not recommended that a virtual function change the default value for this reason. Unfortunately I don’t know any compiler that warns on this construct.


    The technical explication is that there are two ways of dealing with default argument:

    • create a new function to act as trampoline: void A::func() { func(1); }
    • add-in the missing argument at the call site a.func() => a.func(/*magic*/1)

    If it were the former (and assuming that the A::func was declared virtual as well), then it would work like you expect. However the latter form was elected, either because issues with virtual were not foreseen at the time or because they were deemed inconsequential in face of the benefits (if any…).

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

Sidebar

Related Questions

What does a rack handler do exactly, i.e. can someone explain in pseudo-code the
Can someone please explain this piece of code? struct Class { boost::function<void()> member; };
Can someone explain the following two outputs? Code 1: console.log(itemsAry); //loadNextItem(); function loadNextItem(){ var
Why does this code produce the same output for both method calls? I would
Can someone explain these few lines of MSIL? Why does it move a value
Can someone explain to me why my code: string messageBody = abc\n + stringFromDatabaseProcedure;
The following is a python code using the subprocess module. Can someone explain what
Can someone please explain in plain language how this code works to give a
Can someone explain to me why the following code compiles OK in Java? char
Can someone explain this code to me a little so I have a firmer

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.