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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:18:41+00:00 2026-05-16T15:18:41+00:00

If I declare a base class (or interface class) and specify a default value

  • 0

If I declare a base class (or interface class) and specify a default value for one or more of its parameters, do the derived classes have to specify the same defaults and if not, which defaults will manifest in the derived classes?

Addendum: I’m also interested in how this may be handled across different compilers and any input on “recommended” practice in this scenario.

  • 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-16T15:18:42+00:00Added an answer on May 16, 2026 at 3:18 pm

    Virtuals may have defaults. The defaults in the base class are not inherited by derived classes.

    Which default is used — ie, the base class’ or a derived class’ — is determined by the static type used to make the call to the function. If you call through a base class object, pointer or reference, the default denoted in the base class is used. Conversely, if you call through a derived class object, pointer or reference the defaults denoted in the derived class are used. There is an example below the Standard quotation that demonstrates this.

    Some compilers may do something different, but this is what the C++03 and C++11 Standards say:

    8.3.6.10:

    A virtual function call (10.3) uses
    the default arguments in the
    declaration of the virtual function
    determined
    by the static type of the pointer or reference denoting the object. An
    overriding function in a derived
    class does not acquire default arguments from the function it
    overrides. Example:

    struct A {
      virtual void f(int a = 7);
    };
    struct B : public A {
      void f(int a);
    };
    void m()
    {
      B* pb = new B;
      A* pa = pb;
      pa->f(); //OK, calls pa->B::f(7)
      pb->f(); //error: wrong number of arguments for B::f()
    }
    

    Here is a sample program to demonstrate what defaults are picked up. I’m using structs here rather than classes simply for brevity — class and struct are exactly the same in almost every way except default visibility.

    #include <string>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    
    using std::stringstream;
    using std::string;
    using std::cout;
    using std::endl;
    
    struct Base { virtual string Speak(int n = 42); };
    struct Der : public Base { string Speak(int n = 84); };
    
    string Base::Speak(int n) 
    { 
        stringstream ss;
        ss << "Base " << n;
        return ss.str();
    }
    
    string Der::Speak(int n)
    {
        stringstream ss;
        ss << "Der " << n;
        return ss.str();
    }
    
    int main()
    {
        Base b1;
        Der d1;
    
        Base *pb1 = &b1, *pb2 = &d1;
        Der *pd1 = &d1;
        cout << pb1->Speak() << "\n"    // Base 42
            << pb2->Speak() << "\n"     // Der 42
            << pd1->Speak() << "\n"     // Der 84
            << endl;
    }
    

    The output of this program (on MSVC10 and GCC 4.4) is:

    Base 42
    Der 42
    Der 84
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let us assume I have two classes: class Base{}; class Derived: public Base{}; none
I have an abstract base class and I want to declare a field or
While we can inherit from base class/interface, why can't we declare a List<> using
In C++, is it possible to have a base plus derived class implement a
Suppose I have a base and derived class: class Base { public: virtual void
I know it is a good practice to declare virtual destructors for base classes
Given the base class A and the derived class B: class A { public:
Quirky question: Imagine that I have a base class called BaseFoo. I have an
Do you have to declare methods replacing a pure virtual function in a base
I am extending an existing C++ project. I have a base class that derives

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.