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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:12:08+00:00 2026-05-25T19:12:08+00:00

public: const int x; base():x(5){} }; class der : public base { public: der():x(10){}

  • 0
public:
    const int x;
    base():x(5){}

};

class der : public base {
public:
    der():x(10){}
};

der d;

My aim is when instance of base class is created it will initialise x as 5 and when instance of der class is created it will initialise x as 10. But compiler is giving error.
As x is inherited from class base, why is it giving error?

  • 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-25T19:12:08+00:00Added an answer on May 25, 2026 at 7:12 pm

    You can’t initialize a base class member in the initializer list for a constructor in the derived class. The initializer list can contain bases, and members in this class, but not members in bases.

    Admittedly, the standardese for this isn’t entirely clear. 12.6.2/2 of C++03:

    Unless the mem-initializer-id names a nonstatic data member of the
    constructor’s class or a direct or virtual base of that class, the
    mem-initializer is ill-formed.

    It means “(a nonstatic data member of the constructor’s class) or (a direct or virtual base)”. It doesn’t mean “a nonstatic data member of (the constructor’s class or a direct or virtual base)”. The sentence is ambiguous, but if you took the second reading then you couldn’t put bases in the initializer-list at all, and the very next sentence in the standard makes it clear that you can.

    As for why it’s not allowed, that’s a standard rationale question and I’m guessing at the motives of the authors of the standard. But basically because it’s the base class’s responsibility to initialize its own members, not the derived class’s responsibility.

    Probably you should add an int constructor to base.

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

Sidebar

Related Questions

#include <iostream> #include <conio.h> using namespace std; class Base { int a; public: Base(const
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
I heave roughly speaking the following hierarchy class Base { public: virtual int value()
I've got this trivial class hierarchy: class Base { public: virtual int x( )
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
Given this code: class base { public: string foo() const; // Want this to
I've got two classes here. A Base class: class A { int x; public:
In the following method, public void InspectList(IList<int> values) { if(values != null) { const
How is memory allocated when I use: public class MyClass { public const string

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.