样式:在派生类中使用成员引用? [英] Style: use of member references in derived classes?

查看:52
本文介绍了样式:在派生类中使用成员引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从库中的基类派生出来的,库供应商是

,希望我在派生类中添加一个''string&''成员。或者,更确切地说,它们提供了两个虚函数:


1 void setstr(const std :: string& str)

2 std :: string& getstr(void)


所以我明显的实现是:


class foo:vendor_bar {

string& str;

public:

virtual void setstr(const string& str_){str = str_; }

虚拟字符串& getstr(void){return str; }

};


我对此感觉不好。拥有这样的

参考会员是个好主意吗?我不能想到任何具体的问题,

但我宁愿使用某种智能指针。


另一个因素是string实际上最初由

我(图书馆用户)提供,在图书馆的其他地方没有使用,只需

在''setstr中回复给我''打电话;我自己在层次结构中进一步使用它。

。鉴于此,是否有更好的方法可以设计库

,而不是在字符串中进行硬连接

实现?


干杯


AL

解决方案

* Andy Lomax:

我是从库中的基类派生出来的,库供应商希望我在派生类中添加一个''string&''成员。或者,更准确地说,它们提供了两个虚函数:

1 void setstr(const std :: string& str)
2 std :: string& getstr(void)


糟糕的设计。


所以我明显的实现是:

class foo:vendor_bar {
字符串& str;
public:
virtual void setstr(const string& str_){str = str_; }
虚拟字符串& getstr(void){return str; }
};




将该成员声明更改为


string str;


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


< blockquote> 2005年6月24日星期五,格林威治标准时间10:07:50, al***@start.no (Alf P. Steinbach)

写道:

* Andy Lomax:

我是从库中的基类和库中派生出来的供应商希望我在派生类中添加一个''string&''成员。或者,更准确地说,它们提供了两个虚函数:

1 void setstr(const std :: string& str)
2 std :: string& getstr(void)



糟糕的设计。




你有什么特别的理由说它不好吗?

所以我明显的实现是:

class foo:vendor_bar {
string& str;
public:
virtual void setstr(const string& str_){str = str_; }
虚拟字符串& getstr(void){return str; }
};



将成员声明更改为

string str;




嗯...问题是,这个类可能被实例化数千次,

都包含完全相同的字符串。还有其他想法吗?


谢谢 -


AL


" Andy Lomax" ; < AB *** @ [127.0.0.1] GT;在消息中写道

新闻:ea ******************************** @ 4ax.com

2005年6月24日星期五,格林威治标准时间10:07:50, al***@start.no (Alf P. Steinbach)
写道:

* Andy Lomax:

我是从库中的基类派生出来的,图书馆供应商
希望我在我的派生类中添加一个''string&''成员。或者,更确切地说,它们提供了两个虚函数:

1 void setstr(const std :: string& str)
2 std :: string& getstr(void)



糟糕的设计。



你有什么特别的理由说它不好吗?

所以我明显的实现是:

class foo:vendor_bar {
string& str;
public:
virtual void setstr(const string& str_){str = str_; }
虚拟字符串& getstr(void){return str; }
};



将成员声明更改为

string str;



嗯。 ..问题是,这个类可能被实例化了数千次,
都包含完全相同的字符串。还有其他想法吗?




如果你只需要一个字符串用于整个班级,那么

做的显而易见的事就是让它成为一个静态的会员。


-

John Carson


I''m deriving from a base class in a library, and the library vendor is
expecting me to add a ''string&'' member in my derived class. Or, to be
more precise, they have provided two virtual functions:

1 void setstr(const std::string& str)
2 std::string& getstr(void)

So my obvious implementation is:

class foo : vendor_bar {
string& str;
public:
virtual void setstr(const string& str_) { str = str_; }
virtual string& getstr(void) { return str; }
};

I''ve got a bad feeling about this. Is it a good idea to have a
reference member like this? I can''t think of any specific problems,
but I''d rather use a smart pointer of some sort.

Another factor is that the string is actually originally provided by
me (the library user), isn''t used elsewhere in the library, and simply
comes back to me in the ''setstr'' call; I use it myself further down in
the hierarchy. Given this, is there some better way that the library
could have been designed, rather than hard-wiring in a string
implementation?

Cheers

AL

解决方案

* Andy Lomax:

I''m deriving from a base class in a library, and the library vendor is
expecting me to add a ''string&'' member in my derived class. Or, to be
more precise, they have provided two virtual functions:

1 void setstr(const std::string& str)
2 std::string& getstr(void)
Bad design.

So my obvious implementation is:

class foo : vendor_bar {
string& str;
public:
virtual void setstr(const string& str_) { str = str_; }
virtual string& getstr(void) { return str; }
};



Change that member declaration to just

string str;

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


On Fri, 24 Jun 2005 10:07:50 GMT, al***@start.no (Alf P. Steinbach)
wrote:

* Andy Lomax:

I''m deriving from a base class in a library, and the library vendor is
expecting me to add a ''string&'' member in my derived class. Or, to be
more precise, they have provided two virtual functions:

1 void setstr(const std::string& str)
2 std::string& getstr(void)



Bad design.



Do you have any specific reasons for saying that it''s bad?

So my obvious implementation is:

class foo : vendor_bar {
string& str;
public:
virtual void setstr(const string& str_) { str = str_; }
virtual string& getstr(void) { return str; }
};



Change that member declaration to just

string str;



Hmm... problem is, this class may be instantiated thousands of times,
all containing exactly the same string. Any other ideas?

Thanks -

AL


"Andy Lomax" <ab***@[127.0.0.1]> wrote in message
news:ea********************************@4ax.com

On Fri, 24 Jun 2005 10:07:50 GMT, al***@start.no (Alf P. Steinbach)
wrote:

* Andy Lomax:

I''m deriving from a base class in a library, and the library vendor
is expecting me to add a ''string&'' member in my derived class. Or,
to be more precise, they have provided two virtual functions:

1 void setstr(const std::string& str)
2 std::string& getstr(void)



Bad design.



Do you have any specific reasons for saying that it''s bad?

So my obvious implementation is:

class foo : vendor_bar {
string& str;
public:
virtual void setstr(const string& str_) { str = str_; }
virtual string& getstr(void) { return str; }
};



Change that member declaration to just

string str;



Hmm... problem is, this class may be instantiated thousands of times,
all containing exactly the same string. Any other ideas?



If you only want one string for the whole class, then the obvious thing to
do is make it a static member.

--
John Carson


这篇关于样式:在派生类中使用成员引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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