问题
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:
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;
}
};
回答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.
回答3:
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
来源:https://stackoverflow.com/questions/13787901/ensure-derived-class-constructor-must-call-specific-base-class-method