将恒定成员放在基础中 [英] Putting a constant member in a base

查看:52
本文介绍了将恒定成员放在基础中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个类的层次结构,其中将有一个数据元素是所有后代类共有的
。这个元素(在这种情况下是一个字符串)是
常量,但特定于每个类。所以我有这样的事情:


类基地

{

私人:

虚拟标准:: 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''是常量!什么是
一个人要做什么?有没有办法消除不必要的重复和

同时维护数据元素的常量?


谢谢,


- 丹尼斯

解决方案



" Dennis Jones" < no **** @ nospam.comwrote in message

news:ud9zh.140


II6.87@trnddc07 ...
< blockquote class =post_quotes>
你好,


我有一个类的层次结构,其中有一个数据元素

是常见的所有后代班级。这个元素(在这种情况下是一个字符串)

是常量,但特定于每个类。所以我有这样的事情:


类基地

{

私人:

虚拟标准:: 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"){}

};


然而,这不会起作用,因为编译器会抱怨

"'''FString''不是''Derived'的一个不明确的基类"当然,

是真实的并且非常有意义。一个不太理想(但仍然合理)的

解决方案是在构造函数的主体中初始化''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(); }

};

谢谢,

- 丹尼斯


Dennis Jones写道:


你好,


我有一个类的层次结构,其中有一个数据元素是

对所有后代类都是通用的。这个元素(在这种情况下是一个字符串)是
常量,但特定于每个类。所以我有这样的事情:


类基地

{

私人:

虚拟标准:: 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''是常量!什么是
一个人要做什么?有没有办法消除不必要的重复和

同时维护数据元素的常量?


谢谢,


- 丹尼斯



我不确定我理解你在寻找什么但是

你有机会找到这个吗?


班级基地

{

私人:

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 is
common to all descendant classes. This element (a string in this case) is
constant, 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 be
much 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 and
makes perfect sense. A less desirable (but still reasonable) solution would
be 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''s
one to do? Is there a way to eliminate the unnecessary duplication and
maintain the constness of the data element at the same time?

Thanks,

- Dennis

解决方案


"Dennis Jones" <no****@nospam.comwrote in message
news:ud9zh.140


II6.87@trnddc07...

Hello,

I have a hierarchy of classes in which there will be a data element that
is common to all descendant classes. This element (a string in this case)
is constant, 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 be much 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 and makes perfect sense. A less desirable (but still reasonable)
solution would be 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''s
one to do? Is there a way to eliminate the unnecessary duplication and
maintain the constness of the data element at the same time?


P.S. I should have mentioned that due to my employer''s preferences, using a
constructor argument (as shown below) is not desirable, which is why I am
searching 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,
- Dennis


Dennis Jones wrote:

Hello,

I have a hierarchy of classes in which there will be a data element that is
common to all descendant classes. This element (a string in this case) is
constant, 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 be
much 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 and
makes perfect sense. A less desirable (but still reasonable) solution would
be 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''s
one to do? Is there a way to eliminate the unnecessary duplication and
maintain the constness of the data element at the same time?

Thanks,

- Dennis

I am not sure I understand what you are looking for but
by 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 else
Derived( const std::string &val ) : Base(val) {}
};

Hope that helps!


这篇关于将恒定成员放在基础中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆