基础结构而不是类 [英] Base struct instead of class

查看:50
本文介绍了基础结构而不是类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取出C ++在一本Nutshell书中...


例7-18:使用抽象类作为接口规范。

struct Runnable {

virtual void run()= 0;

};


struct Hashable {

virtual size_t hash()= 0;

};


class Thread:public Runnable,public Hashable {

public:

Thread(){start_thread(* this); } $ / $
线程(const Runnable& thread){start_thread(thread); } $ / $
virtual void run();

virtual size_t hash()const {return thread_id(); }

size_t thread_id()const;

...

private:

static void start_thread(const Runnable& ;);

};


使用struct作为接口的优势是什么(纯虚拟

基类) ?

谢谢,马丁

Taken out of C++ In a Nutshell book...

Example 7-18: Using an abstract classes as interface specification.
struct Runnable {
virtual void run() = 0;
};

struct Hashable {
virtual size_t hash() = 0;
};

class Thread : public Runnable, public Hashable {
public:
Thread() { start_thread(*this); }
Thread(const Runnable& thread) { start_thread(thread); }
virtual void run();
virtual size_t hash() const { return thread_id(); }
size_t thread_id() const;
...
private:
static void start_thread(const Runnable&);
};

What would the advantage be of using struct as an interface (pure virtual
base class)?
Thanks,Martin

推荐答案

简洁。因此你不必输入:


class Runnable

{

public:

virtual void run()= 0;

};


否则没有优势。我个人只在需要

来对变量进行分组时才使用struct,这样我就可以把它写成一个字节块到一个文件或者从文件中读取
。否则我会使用课程,如果打算包含

功能(例如方法)。


Tom


" ; Marcin Vorbrodt" < MV ***** @ eos.ncsu.edu>在消息中写道

news:bj ********** @ uni00nw.unity.ncsu.edu ...
brevity. Thus you don''t have to type:

class Runnable
{
public:
virtual void run() = 0;
};

Otherwise there is no advantage. Personally I use struct only when I want
to group variables so that I might write it as a block of bytes to a file or
read from a file. Otherwise I use class if the intention is to contain
functionality (e.g. methods).

Tom

"Marcin Vorbrodt" <mv*****@eos.ncsu.edu> wrote in message
news:bj**********@uni00nw.unity.ncsu.edu...
在果壳中取出C ++本书...示例7-18:使用抽象类作为接口规范。
struct Runnable {
virtual void run()= 0;
};

struct Hashable {
virtual size_t hash()= 0;
};

类Thread:public Runnable,public Hashable {
public:
Thread(){start_thread(* this);线程(const Runnable& thread){start_thread(thread); } void void run();
virtual size_t hash()const {return thread_id(); }
size_t thread_id()const;
...
private:
static void start_thread(const Runnable&);
};
使用struct作为接口(纯虚基本类)的优势是什么?
谢谢,Martin
Taken out of C++ In a Nutshell book...

Example 7-18: Using an abstract classes as interface specification.
struct Runnable {
virtual void run() = 0;
};

struct Hashable {
virtual size_t hash() = 0;
};

class Thread : public Runnable, public Hashable {
public:
Thread() { start_thread(*this); }
Thread(const Runnable& thread) { start_thread(thread); }
virtual void run();
virtual size_t hash() const { return thread_id(); }
size_t thread_id() const;
...
private:
static void start_thread(const Runnable&);
};

What would the advantage be of using struct as an interface (pure virtual
base class)?
Thanks,Martin



Thomas J. Clancy写道:
Thomas J. Clancy wrote:
简洁。因此,你不必输入:
brevity. Thus you don''t have to type:



< snip>


请不要顶一下。重新阅读常见问题解答第5部分,发布

指南。你也可以在几乎任何网络礼节中找到这个规则,包括RFC 1855.

http://www.parashift.com/c++-faq-lite/

-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


<snip>

Please don''t top-post. Re-read section 5 of the FAQ for posting
guidelines. You could also find this rule in pretty much any netiquette
reference, including RFC 1855.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.




" Kevin Goodsell" <我们********************* @ neverbox.com>在留言中写道

news:h6 *************** @ newsread4.news.pas.earthlin k.net ...

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:h6***************@newsread4.news.pas.earthlin k.net...
Thomas J. Clancy写道:
Thomas J. Clancy wrote:
简洁。因此,您不必输入:
brevity. Thus you don''t have to type:


< snip>

请不要顶​​尖。重新阅读发布
指南的常见问题解答第5部分。您也可以在任何网络礼节参考中找到此规则,包括RFC 1855.

http://www.parashift.com/c++-faq-lite/
-Kevin


<snip>

Please don''t top-post. Re-read section 5 of the FAQ for posting
guidelines. You could also find this rule in pretty much any netiquette
reference, including RFC 1855.

http://www.parashift.com/c++-faq-lite/

-Kevin




啊是的,我忘记了在大多数

新闻组中发布这个特殊的怪癖。我发现非顶级帖子令人震惊,除非我正在处理

特定的已发布问题。但是,唉,我是

少数派。尽管如此,我觉得有理由认为有人会问一个简单的问题,而不是必须向下扫描才能找到答案,如果它是在他/她的b / b $ b $ top可以立即看到答案 - 因此即时满足。


无论如何,因为它似乎让你和其他人感到愤怒,我会,对我自己感到沮丧,b $ b令人沮丧,遵守。


T. Clancy



Ah yes, I''d forgotten about this particular quirk of posting in most
newsgroups. I find non-top posting appalling unless I am addressing
specific pieces of the posted question or questions. But alas, I am in the
minority. Still, I find it logical that should someone ask one simple
question, rather than have to scan down to find the answer, if it is at the
top he/she can immediately see the answer--thus instant gratification.

Anyway, since it seems to anger you and perhaps others I will, to my own
dismay, comply.

T. Clancy


这篇关于基础结构而不是类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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