如何在两个以上的.h文件中定义一个类? [英] How to define a class in more than two .h files?

查看:55
本文介绍了如何在两个以上的.h文件中定义一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个大班。

我想在这个班级的不同文件中定义。

怎么做?

谢谢。

I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.

推荐答案

ronic写道:
ronic wrote:
我正在创建一个大班。


你确定你不能将它分成几个较小的类吗?

我想在这个类的不同文件中定义。
I am creating a big class.
Are you sure you can''t split it up into several smaller classes?
and I want to define in different files for this class.




定义什么?类本身或其成员函数?后者很容易就是b $ b。您可以简单地制作几个包含

函数的实现文件。前者是可能的,但我会反对它,

,因为它使代码可读性降低。它可以是这样的:


//foo.h

class Foo

{

公开:


#include" foo_functions1.h"

#include" foo_functions2.h"

#include" foo_functions3.h"

};

//foo_functions1.h


void someFunc(int param);

void anotherFunc()const;

// ...


等等。



Define what? The class itself or its member functions? The latter is
easy. You can simply make several implementation files that contain the
functions. The former would be possible, but I''d advice against it,
since it makes the code less readable. It could go something like:

//foo.h
class Foo
{
public:

# include "foo_functions1.h"
# include "foo_functions2.h"
# include "foo_functions3.h"
};
//foo_functions1.h

void someFunc(int param);
void anotherFunc() const;
//...

and so on.

ronic写道:
ronic wrote:
我正在创建一个大班。


大班通常是设计不佳的标志。你确定你真的需要这么大的课吗?在回答之前请三思。

我想在这个类的不同文件中定义。


我是学校的一个组件,一个文件其中组件是一个或

更强烈相关的类。因此对我来说听起来很亵渎..... / b
甚至想到在一个以上的文件中拆分一个单独的类。

怎么做?
I am creating a big class.
Big classes are usually a sign of poor design. Are you sure you really
need such a big class? Think twice before answering.
and I want to define in different files for this class.
I am of the school "one component, one file" where "component" is one or
more strongly related classes. Therefore it sound blasphemous to me to
even think of spliting one single class in more than one file.
How to do it?




*非常不情愿*,_only_方式是写这个:


class BigBlasphemousClass

{

#include" BigBlasphemousClass1.h"

#include" BigBlasphemousClass2.h"

};


但仅仅是寻找在这样的事情让我颤抖。


问候,


Alberto Barbati



*Very reluctantly*, the _only_ way is to write this:

class BigBlasphemousClass
{
#include "BigBlasphemousClass1.h"
#include "BigBlasphemousClass2.h"
};

but merely looking at such thing gives me shivers.

Regards,

Alberto Barbati




" ronic" < IA *** @ sohu.com>在消息中写道

news:35 ************************** @ posting.google.c om ...

"ronic" <ia***@sohu.com> wrote in message
news:35**************************@posting.google.c om...
我正在创建一个大班。
我想在这个班级的不同文件中定义。
怎么做?
谢谢。
I am creating a big class.
and I want to define in different files for this class.
How to do it?
Thanks.




一个大班可能是糟糕的设计。

不知道如何把它放在不同的文件中意味着你是一个没经验的人



的程序员没有读太多,这再次暗示这个类可能是一个糟糕的

设计。


我知道它没有直接帮助,但我保证,从长远来看,

你最好只填充所有文件并花时间

保存阅读C ++设计书,所以你下次就可以这样做。


假设你忽略了我的建议:

如果问题是声明(.h)文件太大了:

1.不要使用内联方法。

2.如果你使用内联方法 - 在包含的sparate文件中定义它们

来自主.h


如果问题只是.cpp / .cxx等文件的大小那么只是把它分成两个b $ b - 那个

是没有(实际)的理由不这样做。



A big class is probably bad design.
Not knowing how to put it in different files means you are an inexperienced
programmer who
hasn''t read much which again implies that the class is probably a bad
design.

I know it''s not directly helpful but I promise that in the long term
you''ll be better off just stuffing it all one file and spending the time
saved reading C++ design books so you do it right next time.

Assuming that you ignore my advice:
If the problem is that the declarations (.h) file is too big then:
1. Don''t use inline methods.
2. If you do use inline methods - define them in a sparate file included
from the main .h

If the problem is just the size of the .cpp/.cxx etc file then just split it
in two - there
is no (practical) reason not to.


这篇关于如何在两个以上的.h文件中定义一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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