我删除了剩余的废话。在尝试告知毫无戒心的新手之前,请先阅读多态性 。 V Hi, I have a problem using member functions in derived classes thatoverride virtual member functions of base classes. The following pieces of (simplified) code don''t work. Can anybody giveme some hints on what might be wrong? // BEGIN OF SAMPLE CODEclass Strategy{public:Strategy() {}virtual void run() { cout<<"Please override!"; }}; class StrategyA: public Strategy{public:StrategyA() {}void run();};void StategyA::run(){cout <<"Functionality A";}class SomeClass{public:SomeClass(Strategy s); void doSomething(); private:Strategy s_;}; SomeClass::SomeClass(Strategy s): s_(s) {} SomeClass::doSomething(){s_.run();}int main(){StategyA s;SomeClass c(s); c.doSomething(); return 0;}// END OF SAMPLE CODEThe above program will print out "Please override!" instead of"Functionality A". Scanning through Stroustrup I can''t seem to findwhat is wrong. An additional remark: I know it is better to declare the function"run" in Strategy to be pure virtual (virtual void run() = 0;) butthat won''t compile. The compiler gives error in SomeClass'' constructorand private member declaration. What do I forget? Thanks In Advance, With Kind Regards Ruben Van Havermaet. 解决方案 Declaring a pure virtual function makes the class an ADT (abstract datatype). Because you cannot create an object of an ADT, it must have adefault constructor (but that''s already there). The problem comes fromtrying to give SomeClass a Strategy member. When you do this, a Strategyobject is "constructed" inside SomeClass, which is an error. The SomeClassconstructor error is basically the same; it comes from trying to initializean object of the Strategy ADT. Here''s my version of your code. It should have everything working. #include <iostream> using std::cout; class Strategy { public: Strategy() {} virtual void run() { cout<<"Please override!"; } }; class StrategyA: public Strategy { public: StrategyA() {} virtual void run(); }; void StrategyA::run() { cout <<"Functionality A\n"; } class SomeClass { public: SomeClass() { } void doSomething(); private: StrategyA s_; }; void SomeClass::doSomething() { s_.run(); } int main() { SomeClass c; c.doSomething(); return 0; } //good luck! //mike tyndallIncorrect. There are three and only three instances in which you may not specify areturn type (and in all three instances it is compulsory that you don''t): a) Constructors b) Destructor c) Type conversion operatorsclass Blah{public: Blah(); ~Blah(); operator int();};-JKop Where did you get this nonsense? it''s usually best just to put the constructor/destructor definitions inside the class like this: SomeClass(Strategy s) : s_(s) { } Also, initializing a data member like this has no effect.What are you talking about? The private member variable Strategy s_ is already a Strategy. "Initializing" it with a StrategyA has no effect.Are you out of your mind? There is no data that needs to be initialized, andWhat if I later add data to be initialised? you can''t initialize a object with another object anyway.What???? Change your constructor to: [...] I removed the rest of your nonsense. Please read up on polymorphismbefore attempting to advise unsuspecting newbies. V 这篇关于覆盖子类中的虚拟成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 17:08