#include verse class class_name [英] #include verse class class_name

查看:45
本文介绍了#include verse class class_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会假设许多读这篇文章的人永远不会创建类似下面的例子。所以,让我以_ * IF * _为前言,你处于一个

的情况,你必须在使用#includes或转发之间做出选择

在diamond.h中声明每个类,你会选择哪个?为什么?


如果我接近这个例子的

结构的方式存在根本性的错误,我很有兴趣知道。至于偏好和

的口味,我真的很想专注于上面的问题。


//north.cpp

#include" north.h"

名称空间钻石{

北::北(){}

北::〜北( ){}

};


//east.cpp

#include" east.h"

名称空间钻石{

东::东():北(){}

东::〜东(){}

};


//west.cpp

#include" west.h"

namespace diamond {

West :: West():North(){}

West :: ~West(){}

};
< br $>
//south.cpp

#include" south.h"

名称空间钻石{

South ::南():东(),西(){}

南::〜南(){}

};


//diamond.cpp

#include" diamond.h"

名称空间钻石{

Diamond :: Diamond(){}

Diamond :: ~Diamond(){}

};


//north.h

#ifndef DIAMONDNORTH_H

#define DIAMONDNORTH_H

namespace diamond {

类北{

公共:

北();

~North();

};

};

#endif


//east.h

# ifndef DIAMONDEAST_H

#define DIAMONDEAST_H

#include" north.h"

namespace diamond {

class East :虚拟公共北方

{

公开:

东();

~East();

};

};

#endif


//west.h

#ifndef DIAMONDWEST_H

#define DIAMONDWEST_H

#include" north.h"

namespace diamond {

class West:虚拟公共北方

{

公共:

West();

~West( );

};

};

#endif


//south.h

#ifndef DIAMONDSOUTH_H

#define DIAMONDSOUTH_ H $>
#include" east.h"

#include" west.h"

namespace diamond {

class South:虚拟公共东部,虚拟公共西部

{

公共:

南();

~南();

};

};

#endif


// diamond.h

#ifndef DIAMONDDIAMOND_H

#define DIAMONDDIAMOND_H

名称空间钻石

{

class Diamond

{

public:

Diamond();

~Diamond();

私人:

东e;

北n;

西w;

南s;

};

};


-

p-> m = =(* p).m == p [0] .m
http:// www .kdevelop.org
http://www.suse.com
http://www.m ozilla.org

I will assume many people reading this would never create anything similar
to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?

If there is something fundamentally wrong with the way I''ve approached the
structure of this example, I am interested to know. As for preferences and
tastes, I would really like to stay focused on the question above.

//north.cpp
#include "north.h"
namespace diamond {
North::North(){}
North::~North(){}
};

//east.cpp
#include "east.h"
namespace diamond {
East::East() : North(){}
East::~East(){}
};

//west.cpp
#include "west.h"
namespace diamond {
West::West() : North(){}
West::~West(){}
};

//south.cpp
#include "south.h"
namespace diamond {
South::South(): East(), West(){}
South::~South(){}
};

//diamond.cpp
#include "diamond.h"
namespace diamond {
Diamond::Diamond(){}
Diamond::~Diamond(){}
};

//north.h
#ifndef DIAMONDNORTH_H
#define DIAMONDNORTH_H
namespace diamond {
class North{
public:
North();
~North();
};
};
#endif

//east.h
#ifndef DIAMONDEAST_H
#define DIAMONDEAST_H
#include "north.h"
namespace diamond {
class East : virtual public North
{
public:
East();
~East();
};
};
#endif

//west.h
#ifndef DIAMONDWEST_H
#define DIAMONDWEST_H
#include "north.h"
namespace diamond {
class West : virtual public North
{
public:
West();
~West();
};
};
#endif

//south.h
#ifndef DIAMONDSOUTH_H
#define DIAMONDSOUTH_H
#include "east.h"
#include "west.h"
namespace diamond {
class South : virtual public East, virtual public West
{
public:
South();
~South();
};
};
#endif

//diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H
namespace diamond
{
class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
我会假设很多阅读本文的人永远不会创建类似下面的例子。所以,让我用_ * IF * _作为前言,你必须选择使用#includes或者转发
在diamond.h中声明每个类,你会选择哪个?为什么?


事实证明,在这种情况下,我必须使用#includes。我不是百分百肯定

为什么会这样。我只是知道它没有编译,并且错误地说

类定义不完整。

//diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H
命名空间钻石
{
/ *没有工作* /

类East

class West

班北

班南级钻石
公开:
钻石();
~Diamond();
私人:
东e;
北n;
西w;
南s;
};
};
I will assume many people reading this would never create anything similar
to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?
As it turns out, in this case I have to use the #includes. I''m not 100% sure
why that is. I just know it didn''t compile, and gave an error saying the
class definitions were incomplete.
//diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H
namespace diamond
{ /*didn''t work*/
class East
class West
class North
class South class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};




-

p-> m ==(* p).m == p [0] .m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org



--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org


Steven T. Hatton写道:
Steven T. Hatton wrote:
我将假设很多读这篇文章的人永远不会创建类似下面的例子。所以,让我用_ * IF * _作为前言,你必须选择使用#includes或者转发
在diamond.h中声明每个类,你会选择哪个?为什么?


事实证明,在这种情况下,我必须使用#includes。我不是百分百肯定

为什么会这样。我只是知道它没有编译,并且错误地说

类定义不完整。

//diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H
命名空间钻石
{
/ *没有工作* /

类East

class West

班北

班南级钻石
公开:
钻石();
~Diamond();
私人:
东e;
北n;
西w;
南s;
};
};
I will assume many people reading this would never create anything similar
to the example below. So let me preface this with _*IF*_ you were in a
situation where you had to chose between using #includes or forward
declaring each class in diamond.h, which would you choose? Why?
As it turns out, in this case I have to use the #includes. I''m not 100% sure
why that is. I just know it didn''t compile, and gave an error saying the
class definitions were incomplete.
//diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H
namespace diamond
{ /*didn''t work*/
class East
class West
class North
class South class Diamond
{
public:
Diamond();
~Diamond();
private:
East e;
North n;
West w;
South s;
};
};




-

p-> m ==(* p).m == p [0] .m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org



--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org


Steven T. Hatton写道:

[snip]


Pref前瞻性声明。如果您在其中包含(不必要的)其他标题

,您将减慢编译速度而没有任何实际好处。
Steven T. Hatton wrote:
[snip]

Prefer forward declarations. If you include (unnecessary) other headers
in yours you will slow down compilation without any real benefit.
/ *不起作用* / <东班级
班级西班级
班级南部
/*didn''t work*/
class East
class West
class North
class South



[snip]


[snip]

East e;
North n;
West w;
South s;
East e;
North n;
West w;
South s;



[snip]


那是因为你按价值存储了它们。编译器需要确定你的班级大小

..如果没有关于East,West等的完整

数据,怎么能为钻石做到这一点。


您只能在以下

的情况下(可能错过了几个)获得前瞻声明:


1)你的类/标题永远不会*按值包含*前向声明的类。


2)你的类/标题从来没有一个方法/函数可以转发

按值声明类(引用或指针很好,因为返回

它的值)。


3)你的标题从不使用前向声明的类适用范围

分辨率(例如东方::无论如何)。


4)您不会继承前方声明的类。

为了解决这个问题,您可以使用编译器防火墙习语:


// diamond.h

#ifndef DIAMONDDIAMOND_H

#define DIAMONDDIAMOND_H

namespace diamond
{

类钻石

{

公开:

钻石();

~Diamond();


私人:

//如果这些不是私人的,那么他们需要特殊的

//处理以制作下面指针的副本。

Diamond(const Diamond& ); //未实施

Diamond& operator =(const Diamond&); //未实现。


struct Impl;

Impl * impl_;

};

}


#endif //!DIAMONDDIAMOND_H


// diamond.cpp

#include" diamond。 h"

#include" north.h"

#include" south.h"

#include" east.h"

#include" west.h"


名称空间钻石

{

struct Diamond: :Impl

{

//你想要的任何构造者/额外成员。


East e;

北n;

西w;

南s;

};


Diamond :: Diamond():impl_(new Impl){}

Diamond :: ~Diamond(){delete impl_; }

}


我个人使用boost :: scoped_ptr来保存impl_指针所以我

没有担心删除它。另请注意,如果对象可以复制,则需要额外的

护理。


如果你想使用scoped_ptr,你必须记住你的

构造函数和析构函数*在Impl类定义之后。否则

编译器会抱怨类型不完整。


另请参阅:

http://www.gotw.ca/gotw/024.htm
http://www.gotw.ca/gotw/028.htm
< a rel =nofollowhref =http://c2.com/cgi/wiki?PimplIdiomtarget =_ blank> http://c2.com/cgi/wiki?PimplIdiom


......还有许多谷歌提供的其他产品。本周档案的大师是

充满了这种东西( www.gotw.ca/gotw)


- Pete


[snip]

That''s because you stored them by value. The compiler needs to determine
the size of your class.. how can it do this for diamond without the full
data about East, West, etc.

You can only get away with forward declarations in the following
circumstances (might have missed a few):

1) Your class/header never *contains* the forward declared class by value.

2) Your class/header never has a method/function that takes the forward
declared class by value (reference or pointer is fine, as is returning
it by value).

3) Your header never uses the forward declared class for scope
resolution (e.g. East::whatever).

4) You do not inherit from the forward declared class.

To get around this you can use the compiler firewall idiom:

// diamond.h
#ifndef DIAMONDDIAMOND_H
#define DIAMONDDIAMOND_H

namespace diamond
{
class Diamond
{
public:
Diamond();
~Diamond();

private:
// If these aren''t private then they need special
// handling to make a copy of the pointer below.
Diamond( const Diamond& ); // Not implemented
Diamond& operator=( const Diamond& ); // Not implemented.

struct Impl;
Impl* impl_;
};
}

#endif // !DIAMONDDIAMOND_H

// diamond.cpp
#include "diamond.h"
#include "north.h"
#include "south.h"
#include "east.h"
#include "west.h"

namespace diamond
{
struct Diamond::Impl
{
// Any constructor/extra members you want here.

East e;
North n;
West w;
South s;
};

Diamond::Diamond() : impl_( new Impl ) {}
Diamond::~Diamond() { delete impl_; }
}

Personally I use a boost::scoped_ptr to hold the impl_ pointer so I
don''t have to worry about deleting it. Note also that you need extra
care if the object will be copyable.

If you do want to use scoped_ptr, you must remember to have your
constructor and destructor *after* the Impl class definition. Otherwise
the compiler complains about incomplete types.

See also:

http://www.gotw.ca/gotw/024.htm
http://www.gotw.ca/gotw/028.htm
http://c2.com/cgi/wiki?PimplIdiom

...and many others available from google. The guru of the week archive is
filled with this kind of stuff (www.gotw.ca/gotw).

-- Pete


这篇关于#include verse class class_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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