编写头文件的规则是什么? [英] what are the rules for writing header files?

查看:78
本文介绍了编写头文件的规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好,


我写了一个包含很多私人数据成员的课程。我要把它放在

it在一个单独的dll文件中。

现在当我在编写我的主程序时链接该文件

模块,natuarally我必须使用开发类的头文件

由我来访问它的功能。

那么头文件中应该有什么?

只有该类的公共方法声明/数据成员或者

整个班级布局及其所有私人会员?



IMO私人部分限制班级的可访问性和

该类的用户是从

类的确切布局中抽象出来的。所以不应该显示我的内部布局

通过将私有变量放在那个

类的头文件中。




我遇到了严重的崩溃问题我宣布上课了那个

头文件中的私有成员?但是当

i将类的确切布局正确放入头文件中时,一切正常。

(我已经使用
范围解析运算符)


谢谢和问候,

Yogesh

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。版主。第一次海报:做到这一点! ]

解决方案


yp ********* @ indiatimes.com 写道:

大家好,

我写过很多私人课程数据成员。我把它放在一个单独的dll文件中。
现在当我在编写我的主程序模块时链接该文件时,natuarally我必须使用该类的头文件开发
由我来访问它的功能。
那么头文件中应该有什么?
只有该类的公共方法声明/数据成员或
整个布局该课程及其所有私人会员?
(IMO私人部分限制了课程的可访问性,并且该课程的用户从
的确切布局中抽象出来。所以不应该通过将私有变量放在那个
c的头文件中来显示我的
类的内部布局。 lass。

当我在头文件中声明没有
私有成员的类时,我遇到了严重的崩溃问题?但是当我将类的确切布局正确地放在头文件中时,一切正常。
(我已经使用范围解析运算符在单独的cpp文件中定义了所有公共方法)




片刻之后,你可能会对旧的C ++没有概念

dll感到恐惧。 (或共享对象或任何其他类型的可加载库)。


无论如何,当您导出类时,您只想导出界面

。这意味着以下两点之一:


- 使用抽象基类和工厂

- 使用pImpl


我不知道你对这门语言的了解,但基本上他们的意思是

如下:


- 抽象基类:至少有一个纯虚方法。在您的

案例中,可能所有这些都是纯虚拟的。确保你还有一个

虚拟析构函数。

- 类工厂:这通常是一个对象但可以是一个函数

来生成实例这个类。


请注意,由于必须使用new分配类,因此需要将
作为已定义的删除者。你的图书馆应该提供一个删除器。

(同样它可以是一个功能或功能)。然后使用

类的库必须使用删除器来销毁对象。一个boost :: shared_ptr

可以删除。请注意,对于自定义删除器,一个选项是

使这个具体(在通用标头中),但它自己有一个抽象的

指针。它调用抽象指针上的一个方法(当operator()

被调用时)同时应该删除它自己,所以删除器

具有相同的寿命作为对象。


- pImpl使用指向实现的指针。只有

向前声明的类型(但在你的库的实现中已知

类)。然后,您的班级将把所有实现转发给此pImpl

类。请注意,由于pImpl是一个指针,您必须重载

析构函数,并处理赋值和复制构造(或

禁用它们)。这种方法的优点是允许用户在没有工厂的情况下直接创建一个类(因此能够更好地管理它们,并且这些类可以摧毁它们)也可以在堆上创建


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


文章< 11 ********************* @ o13g2000cwo.googlegroups 。 com>,
yp*********@indiatimes.com 写道< blockquote class =post_quotes>我写了一个包含许多私有数据成员的类。我将它放在一个单独的dll文件中。
现在当我在编写主程序时链接该文件
模块,natuarally我必须使用我开发的类的头文件来访问它的功能。
那么头文件中应该有什么?
只有公共方法声明/该类的数据成员或该类的整个布局及其所有私有成员?
(IMO私有部分限制了类的可访问性和用户的权限该类的抽象是从
类的确切布局中抽象出来的。所以不应该通过将私有变量放在那个
的头文件中来显示我的
类的内部布局。上课。

我在没有头文件中的私有成员?但是当我将类的确切布局正确地放在头文件中时,一切正常。
(我已经使用范围解析运算符在单独的cpp文件中定义了所有公共方法)




您的问题与使用DLL无关。问题非常简单,编译器需要知道一些有关数据的细节,以便

它可以为构造函数预留足够的存储空间来构建一个

实例。


如果你愿意走在未定义的行为领域,你可以

(虽然我真的不建议)只需声明一个足够大的数组

的char作为头文件中类的私有成员。那么你

可以在

实现文件中提供完整正确的类定义,而不包括标题。当然,如果(动态)链接器足够聪明地使用

某些东西(如校验和)以确保
中的类定义,那么你可能仍会遇到问题。
单独的TU是相同的。


-

Francis Glassborow ACCU

作者''你可以做到!''参见 http://www.spellen.org/youcandoit
关于项目构想和贡献: http://www.spellen.org / youcandoit / projects

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]


yp ****** ***@indiatimes.com 写道:

我写了一个包含许多私有数据成员的类。我把它放在一个单独的dll文件中。
现在当我在编写我的主程序
模块时链接该文件时,我必须使用我开发的类的头文件来访问它的功能。
所以头文件中应该有什么?
只有该类的公共方法声明/数据成员或该类的整个布局及其所有私有成员?


除了课堂定义之外你还会把私人会员放在哪里?

没有其他地方。

(< IMO私有部分限制了类的可访问性,并且类的用户从
类的确切布局中抽象出来。因此不需要显示我的内部布局
通过将私有变量放在该
类的头文件中来实现。




但是,私有成员在那里也不会伤害用户。

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Hello all,

I have written a class with many private data members.and i am putting
it in a separate dll file.
Now when i link that file while writing my main program
module,natuarally i have to use the header file of the class developed
by me to access its functionality.
so what should be there in the header file?
Only the public methods declaration / data members of that class or the
whole layout of the class along with its all private members?
(
IMO the private section restricts the accessibility of the class and
the user of the class is abstracted from the exact layout of the
class.So there should be no need to show the internal layout of my
class by putting the private variable inside the header file of that
class.
)

I encountered severe crasing problem when I declare the class without
the private members in the header file? but everything works fine when
i put the exact layout of the class correctly inside the header file.
(i have defined all the public methods inside a separate cpp file with
the scope resolution operator)

Thanks and Regards,
Yogesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

解决方案


yp*********@indiatimes.com wrote:

Hello all,

I have written a class with many private data members.and i am putting
it in a separate dll file.
Now when i link that file while writing my main program
module,natuarally i have to use the header file of the class developed
by me to access its functionality.
so what should be there in the header file?
Only the public methods declaration / data members of that class or the
whole layout of the class along with its all private members?
(
IMO the private section restricts the accessibility of the class and
the user of the class is abstracted from the exact layout of the
class.So there should be no need to show the internal layout of my
class by putting the private variable inside the header file of that
class.
) I encountered severe crasing problem when I declare the class without
the private members in the header file? but everything works fine when
i put the exact layout of the class correctly inside the header file.
(i have defined all the public methods inside a separate cpp file with
the scope resolution operator)



In a moment you''ll probably get flamed with the old "C++ has no concept
of dlls" (or shared objects or any other kind of loadable library).

Anyway, when you are exporting a class you want to export the interface
only. That means one of two things:

- Use an abstract base class and a factory
- Use a pImpl

I don''t know your knowledge of the language but basically they mean as
follows:

- Abstract base class: has at least one pure virtual method. In your
case probably all of them will be pure virtual. Ensure you also have a
virtual destructor.
- Class factory: This will usually be an object but can be a function
to generate instance of the class.

Note that as the classes have to be allocated with "new", there will
need to be a defined deleter. Your library should provide a deleter.
(Again it can be a function or functior). Then the library using the
class must use the deleter to destroy the object. A boost::shared_ptr
can take a deleter. Note that for a custom deleter, one option is to
make this concrete (in a generic header) but have itself an abstract
pointer. It invokes a method on the abstract pointer (when operator()
is called) which at the same time should delete itself, so the deleter
has the same life-span as the object.

- The pImpl uses a "pointer to implementation" of a type that is only
forwardly declared (but is known in the implementation of your library
class). Your class will then forward all implementation to this pImpl
class. Note that as a pImpl is a pointer, you have to overload the
destructor, and handle also assignment and copy-construction (or
disable them). This method has the advantage of allowing users to
create a class directly without a factory (and therefore to be able to
more eaily manage destroying them) and that these classes can also be
created on the heap.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


In article <11*********************@o13g2000cwo.googlegroups. com>,
yp*********@indiatimes.com writes

I have written a class with many private data members.and i am putting
it in a separate dll file.
Now when i link that file while writing my main program
module,natuarally i have to use the header file of the class developed
by me to access its functionality.
so what should be there in the header file?
Only the public methods declaration / data members of that class or the
whole layout of the class along with its all private members?
(
IMO the private section restricts the accessibility of the class and
the user of the class is abstracted from the exact layout of the
class.So there should be no need to show the internal layout of my
class by putting the private variable inside the header file of that
class.
)

I encountered severe crasing problem when I declare the class without
the private members in the header file? but everything works fine when
i put the exact layout of the class correctly inside the header file.
(i have defined all the public methods inside a separate cpp file with
the scope resolution operator)



Your problem has nothing to do with using a DLL. The issue is very
simple, the compiler needs to know some details about the data so that
it can set aside sufficient storage for the constructor to construct an
instance.

If you are willing to walk in undefined behaviour territory, you could
(though I really do not advise it) simply declare a large enough array
of char as a private member of the class in the header file. Then you
could provide the full and correct class definition in the
implementation file without including the header. Of course you might
still hit problems if the (dynamic) linker is sufficiently clever to use
something such as a checksum to ensure that the class definitions in
separate TUs are identical.

--
Francis Glassborow ACCU
Author of ''You Can Do It!'' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


yp*********@indiatimes.com wrote:

I have written a class with many private data members.and i am putting
it in a separate dll file.
Now when i link that file while writing my main program
module,natuarally i have to use the header file of the class developed
by me to access its functionality.
so what should be there in the header file?
Only the public methods declaration / data members of that class or the
whole layout of the class along with its all private members?
Where else than in the class definition would you put its private members?
There is no other place.
(
IMO the private section restricts the accessibility of the class and
the user of the class is abstracted from the exact layout of the
class. So there should be no need to show the internal layout of my
class by putting the private variable inside the header file of that
class.
)



However, having the private members there won''t hurt the users either.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于编写头文件的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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