本文介绍了将恒定成员放在基础中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我有一个类的层次结构,其中将有一个数据元素是所有后代类共有的。这个元素(在这种情况下是一个字符串)是常量,但特定于每个类。所以我有这样的事情: 类基地 { 私人: 虚拟标准:: string GetString()const = 0; public: void SomeFunction(){GetString(); } }; 类派生:公共基地 { 私人: const std :: string FString; virtual std :: string GetString()const {return FString; } public: 派生() :Base(), FString(" Derived") {} }; 这不是完全不合理的,但我必须复制''FString'' 数据元素及其在每个派生类中的getter函数。如果我可以做这样的事情,那将会更好...... class Base { private: const std :: string FString; std :: string GetString()const {return FString; } public: void SomeFunction(){GetString(); } }; 类派生:公共基地 { public: 派生() :基数(), FString(" Derived"){} }; 然而,这不会起作用,因为编译器会抱怨'''''''''''''''''''''''''''''''''''''''' "当然,这是真的,而b $ b是完全合理的。一个不太理想(但仍然合理)的解决方案是 是在构造函数的主体中初始化''FString'': class派生:public基地 { 公众: 派生() :基地() { FString =" Derived"; } }; 但是当然,这不会起作用,因为''FString''是常量!什么是一个人要做什么?有没有办法消除不必要的重复和 同时维护数据元素的常量? 谢谢, - 丹尼斯 解决方案 P.S.我应该提到,由于我的雇主的偏好,使用 构造函数参数(如下所示)是不可取的,这就是为什么我在寻找b 其他选择: 等级基础 { 私人: const std :: string FString ; std :: string GetString()const {return FString; } public: Base(const std :: string& AString) :FString(AValue){} void SomeFunction(){GetString(); } }; 谢谢, - 丹尼斯 我不确定我理解你在寻找什么但是 你有机会找到这个吗? 班级基地 { 私人: const std :: string FString; std :: string GetString()const {return FString; } public: Base(const std :: string& val):FString(val){} void SomeFunction(){的GetString(); } }; 类派生:公共基地 { public: 派生() :基础(衍生){} //易于使用将值更改为其他内容 派生(const std :: string& val):Base(val){} }; 希望有所帮助! Hello, I have a hierarchy of classes in which there will be a data element that iscommon to all descendant classes. This element (a string in this case) isconstant, but specific to each class. So I have something like this: class Base{private:virtual std::string GetString() const = 0;public:void SomeFunction() { GetString(); }}; class Derived : public Base{private:const std::string FString;virtual std::string GetString() const { return FString; }public:Derived(): Base(),FString( "Derived" ) {}}; This isn''t completely unreasonable, but I do have to duplicate the ''FString''data element and its getter function in every derived class. It would bemuch nicer if I could do something like this instead: class Base{private:const std::string FString;std::string GetString() const { return FString; }public:void SomeFunction() { GetString(); }}; class Derived : public Base{public:Derived(): Base(),FString( "Derived" ) {}}; However, this won''t work because the compiler will complain that "''FString''is not an unabiguous base class of ''Derived''" which, of course, is true andmakes perfect sense. A less desirable (but still reasonable) solution wouldbe to initialize ''FString'' in the constructor''s body: class Derived : public Base{public:Derived(): Base(){FString = "Derived";}}; But of course, this won''t work either because ''FString'' is const! What''sone to do? Is there a way to eliminate the unnecessary duplication andmaintain the constness of the data element at the same time? Thanks, - Dennis 解决方案 P.S. I should have mentioned that due to my employer''s preferences, using aconstructor argument (as shown below) is not desirable, which is why I amsearching for other alternatives: class Base{private:const std::string FString;std::string GetString() const { return FString; }public:Base( const std::string &AString ): FString( AValue ) {}void SomeFunction() { GetString(); }};Thanks,- DennisI am not sure I understand what you are looking for butby any chance you are looking for this? class Base{private:const std::string FString;std::string GetString() const { return FString; }public:Base( const std::string &val ) : FString( val ) {}void SomeFunction() { GetString(); }}; class Derived : public Base{public:Derived(): Base( "Derived" ) {}// for ease of use to change the value to something elseDerived( const std::string &val ) : Base(val) {}}; Hope that helps! 这篇关于将恒定成员放在基础中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 03:55