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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:15:12+00:00 2026-06-15T15:15:12+00:00

In a C++(03) class, I have a member variable, which must be assigned a

  • 0

In a C++(03) class, I have a member variable, which must be assigned a value during object construction. However, only the derived class can compute the required value. As discussed in this post Does C++ require you to initialize base class members from its derived class?, I understand that the derived class cannot initialize a base class member, but an assignment is sufficient for me.

Hence, I provide a method in the base class to assign a value, but I cannot figure out how I can force the derived class to call it. The below sample code illustrates this point.

class Base {
public:
    Base() {}
    setFoo(unsigned inFoo) { foo = inFoo; }
private:
    unsigned foo;
};

class Derived : public Base {
    Derived() : Base() { 
        unsigned desired = ... // do calculations to get the desired value
        setFoo(desired);  // --> how to ensure that derived class calls this?
    }
};

The foo is then used by several other Base class methods. Even if I set a “flag” in the setFoo(), there’s no “post-constructor” method where I can check it. There are several other methods in the base class where foo is used. I could check it there, but it is too tedious, error-prone and inefficient to do so.

My apologies if this has been asked before. I did try to find it here before asking by searching for “Ensure derived class constructor must call specific base class method” and “Force derived class to assign base class member”. I came across some useful posts How can I initialize a const variable of a base class in a derived class' constructor in C++? and Force a Derived Class to use the Constructor of the Base Class , but they unfortunately do not address my question.

Could you please suggest me a suitable approach to address this situation? A compile-time checking solution would be desirable, but if that is not possible, a runtime check would also help. Although I cannot use C++11 for my problem, I would be glad to learn any C++11 solutions as well. Thanks for your consideration.

Edit Of course, documenting this as a requirement is also an option, but I would like to learn if there’s a programmatic solution.

Edit2 I agree with the answers saying that the value should be passed in constructor parameter, and if I had to write Base from scratch, I wouldn’t have asked for this approach. The problem is that due to legacy reasons, Base already has several constructor parameters (9), and I was hoping to avoid adding more (3) by “offloading” them to the constructor body. In the long term though, the code has to be refactored.

Edit3 Sorry for so many edits. I can modify the Base class as I please, but the Derived class is in client code, which I cannot control.

  • 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-15T15:15:13+00:00Added an answer on June 15, 2026 at 3:15 pm

    If the derived class constructor must call setFoo(), then I think that is a design problem. It is better to design the base class, such that it’s constructor takes the desired value as argument, and let the derived class pass the computed value as:

    class Base {
    public:
        Base(unsigned int foo) : m_foo (foo) {}  //define this constructor
    private:
        unsigned int m_foo; //member data
    };
    
    class Derived : public Base {
        Derived() : Base(computeValue()) 
        { 
        }
    private:
        static unsigned int computeValue() //it may take argument(s)
        {
            unsigned int desired = ... //do calculations to get the desired value
            return desired;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a vector member variable, which I fill up
I have a template class. The class has a private member variable which I
I have a member variable that in a class that I want to make
I have a class which have a static member. As I understand, all static
I have simple class with width and height member fields which define number of
I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<
My User entity class have several member variables (e.g. String firstName) which are persisted
I have a class with several member variables, one of which is a DefaultTableModel.
I have a public method which uses a variable (only in the scope of
I have two classes, A and B Class B has a private member variable

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.