3

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.

Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80

3 Answers3

9

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;
    }
};
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    +1 but it's needless confusion to use the same name for member variable and formal argument. *please fix that*. – Cheers and hth. - Alf Dec 09 '12 at 13:51
  • Thanks for your reply. I agree it is not a good design, but the problem is that due to legacy reasons, the base class constructor has several parameters, and I need to extend it. I was hoping to find a way by which I could avoid adding more parameters. In the long term, the code has to be refactored. I suppose I should have made this clear in the question itself, my apologies for leaving that out. – Masked Man Dec 09 '12 at 13:53
  • -1 this approach uses static function, which is bad because the derived class may want to set it's state according to the value it pass to the base class. – Roee Gavirel Dec 09 '12 at 13:56
  • 1
    @RoeeGavirel: The constructor can do that anyway. Also, this is just a basic idea. If it needs to be non-static member, then OP can make it anytime, but that is not better idea either, because the member function might access other *uninitialized* member data eventually, as the object is not fully constructed. If it does, then it is undefined behaviour. – Nawaz Dec 09 '12 at 13:59
  • 2
    @ap: " I was hoping to find a way by which I could avoid adding more parameters" - that's a **different SO question**. post it separately. – Cheers and hth. - Alf Dec 09 '12 at 14:04
  • @Cheersandhth.-Alf Thanks for the advice. I shall do so. – Masked Man Dec 09 '12 at 14:06
2

If a value is required for the construction of an object, it should be a parameter to the constructor.

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

class Derived : public Base {
    Derived() : Base(compute_desired()) { }

};

Here compute_desired can be a member of Derived, or just any function that does the computations.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

I don't think it is possible to force the derived class to call the base class method in it's constructor. You may be able to force the implementation/call through techniques in other responses, but to absolutely make sure that it is done in the constructor of Derived is not possible to the best of my knowledge, unless the base class can be modified

Chubsdad
  • 24,777
  • 4
  • 73
  • 129