C++ 声明类和方法但不声明成员的头文件?
是否可以创建一个 C++ 头文件 (.h) 来声明一个类及其公共方法,但不定义该类中的私有成员? 我发现几页说你应该在标题中声明该类及其所有成员文件,然后在您的 cpp 文件中单独定义方法。 我问这个问题是因为我想要一个在 Win32 DLL 中定义的类,并且我希望它被正确封装:该类的内部实现可能会更改,包括其成员,但这些更改不应影响使用该类的代码。
我想如果我有这个,那么编译器就不可能提前知道我的对象的大小。 但这应该没问题,只要编译器足够聪明,可以使用构造函数,并将指针传递到内存中存储对象的位置,并且永远不要让我运行“sizeof(MyClass)”。
更新:感谢所有回答的人! 看起来 pimpl 习语是实现我所说的目的的好方法。 我将做类似的事情:
我的 Win32 DLL 文件将有一堆单独的函数,如下所示:
void * __stdcall DogCreate();
int __stdcall DogGetWeight(void * this);
void __stdcall DogSetWeight(void * this, int weight);
这是 Microsoft 编写 DLL 文件的典型方式,所以我认为这可能是有充分理由的。
但我想利用 C++ 为类提供的良好语法,因此我将编写一个包装类来包装所有这些函数。 它将有一个成员,即“void * pimpl”。 这个包装类非常简单,我不妨在头文件中声明它并定义它。 但据我所知,这个包装类除了让 C++ 代码看起来更漂亮之外确实没有其他目的。
Is it possible to make a C++ header file (.h) that declares a class, and its public methods, but does not define the private members in that class? I found a few pages that say you should declare the class and all its members in the header file, then define the methods separately in you cpp file. I ask because I want to have a class that is defined in a Win32 DLL, and I want it to be properly encapsulated: the internal implementation of that class might change, including its members, but these changes should not affect code that uses the class.
I guess that if I had this, then it would make it impossible for the compiler to know the size of my objects ahead of time. But that should be fine, as long as the compiler is smart enough to use the constructor and just pass around pointers to the location in memory where my object is stored, and never let me run "sizeof(MyClass)".
Update: Thanks to everyone who answered! It seems like the pimpl idiom is a good way to achieve what I was talking about. I'm going to do something similar:
My Win32 DLL file will have a bunch of separate functions like this:
void * __stdcall DogCreate();
int __stdcall DogGetWeight(void * this);
void __stdcall DogSetWeight(void * this, int weight);
This is the typical way the Microsoft writes their DLL files so I think there is probably good reason for it.
But I want to take advantage of the nice syntax C++ has for classes, so I'll write a wrapper class to wrap up all of these functions. It will have one member, which will be "void * pimpl". This wrapper class will be so simple that I might as well just declare it AND define it in the header file. But this wrapper class really has no purposes other than making the C++ code look pretty as far as I can tell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为你正在寻找的是一种叫做“pimpl idiom”的东西。 要了解这是如何工作的,您需要了解在 C++ 中您可以向前声明类似这样的内容。
因此,转发声明意味着承诺稍后完全声明类型。 它说“我保证,将会有一个叫做 CWidget 的东西。稍后我会告诉你更多关于它的信息。”。
前向声明的规则规定,您可以定义一个指针或对已前向声明的内容的引用。 这是因为指针和引用实际上只是地址——一个尚未定义的东西所在的数字。 出于多种原因,能够在不完全声明的情况下声明指向某些内容的指针是很方便的。
它在这里很有用,因为您可以使用它来使用“pimpl”方法隐藏类的一些内部结构。 Pimpl 的意思是“指向实现的指针”。 因此,您拥有一个实际实现的类,而不是“小部件”。 您在标头中声明的类只是 CImpl 类的传递。 它的工作原理如下:
Thing.cpp 将 CThing 的方法定义为 impl 的传递:
tada! 您已将所有详细信息隐藏在您的 cpp 中,并且您的头文件是一个非常整洁的方法列表。 这是一件伟大的事情。 您可能会发现与上面的模板唯一不同的是人们可能会使用 boost::shared_ptr<> 。 或其他用于实现的智能指针。 会自我删除的东西。
另外,请记住这种方法会带来一些烦恼。 调试可能有点烦人(需要额外级别的重定向来逐步执行)。 创建类的开销也很大。 如果你每堂课都这样做,你就会厌倦所有的打字:)。
I think what you are looking for is something called the "pimpl idiom". To understand how this works, you need to understand that in C++ you can forward declare something like so.
So to forward declare means to promise to fully declare a type later. Its saying "there will be this thing called a CWidget, I promise. I'll tell you more about it later.".
The rules of forward declaration say that you can define a pointer or a reference to something that has been forward declared. This is because pointers and references are really just addresses-a number where this yet-to-be-defined thing will be. Being able to declare a pointer to something without fully declaring it is convenient for a lot of reasons.
Its useful here because you can use this to hide some of the internals of a class using the "pimpl" method. Pimpl means "pointer to implementation". So instead of "widget" you have a class that is the actual implementation. The class you are declaring in your header is just a pass-through to the CImpl class. Here's how it works:
Thing.cpp has CThing's methods defined as pass-throughs to the impl:
tada! You've hidden all the details in your cpp and your header file is a very tidy list of methods. Its a great thing. The only thing you might see different from the template above is that people may use boost::shared_ptr<> or other smart pointer for the impl. Something that deletes itself.
Also, keep in mind this method comes with some annoyances. Debugging can be a tad bit annoying (extra level of redirection to step through). Its also a lot of overhead for creating a class. If you do this for every class, you'll get tired of all the typing :).
使用 pimpl 习惯用法。
Use pimpl idiom.
pimpl 惯用法 将 void* 私有数据成员添加到您的类中,这是一个如果您需要一些快速且有效的东西,这是有用的技术 肮脏的。 然而它也有其缺点。 其中最主要的是它使得在抽象类型上使用多态性变得困难。 有时您可能需要一个抽象基类和该基类的子类,收集指向向量中所有不同类型的指针并调用它们的方法。 此外,如果 pimpl 习惯用法的目的是隐藏类的实现细节,那么它只能几乎成功:指针本身就是一个实现细节。 也许是一个不透明的实现细节。 但仍然有一个实施细节。
pimpl 习惯用法的替代方案可用于从接口中删除所有实现细节,同时提供可多态使用的基本类型(如果需要)。
在 DLL 的头文件(客户端代码 #included 的头文件)中创建一个仅包含公共方法和概念的抽象类,这些方法和概念规定了如何实例化该类(例如,公共工厂方法和克隆方法):
kennel。 h
...Animal 是一个抽象基类因此无法实例化; 无需宣布私人行为人。 虚拟 dtor 的存在确保如果有人
删除
一个Animal*
,正确的子类 dtor 也将被调用。为了实现基类型的不同子类(例如狗和猫),您需要在 DLL 中声明实现级类。 这些类最终派生自您在头文件中声明的抽象基类,并且工厂方法实际上会实例化这些子类之一。
dll.cpp:
现在您已经在标头和文件中定义了接口。 实现细节完全分离,客户端代码根本看不到它。 您可以通过从链接到 DLL 的代码调用头文件中声明的方法来使用它。 这是一个示例驱动程序:
main.cpp:
The pimpl idiom adds a void* private data member to your class, and this is a useful technique if you need something quick & dirty. It has its drawbacks however. Main among those is it makes it difficult to use polymorphism on the abstract type. Sometimes you might want an abstract base class and subclasses of that base class, collect pointers to all the different types in a vector and call methods on them. In addition, if the purpose of the pimpl idiom is to hide the implementation details of the class then it only almost succeeds: the pointer itself is an implementation detail. An opaque implementation detail, perhaps. But an implementation detail nonetheless.
An alternative to the pimpl idiom exists which can be used to remove all of the implementation details from the interface while providing a base type that can be used polymorphically, if needed.
In your DLL's header file (the one #included by client code) create an abstract class with only public methods and concepts which dictate how the class is to be instantiated (eg, public factory methods & clone methods):
kennel.h
...Animal is an abstract base class and so cannot be instantiated; no private ctor needs to be declared. The presence of the virtual dtor ensures that if someone
delete
s anAnimal*
, the proper subclass' dtor will also be called.In order to implement different subclasses of the base type (eg dogs & cats), you would declare implementation-level classes in your DLL. These classes derive ultimately from the abstract base class you declared in your header file, and the factory methods would actually instantiate one of these subclasses.
dll.cpp:
Now you have the interface defined in the header & the implementation details completely seperated out where client code can't see it at all. You would use this by calling methods declared in your header file from code that links to your DLL. Here's a sample driver:
main.cpp:
谷歌“pimple idiom”或“handle C++”。
Google "pimple idiom" or "handle C++".
是的,这可能是一件值得做的事情。 一种简单的方法是使实现类从标头中定义的类派生。
缺点是编译器不知道如何构造您的类,因此您需要某种工厂方法来获取该类的实例。 堆栈上不可能有本地实例。
Yes, this can be a desireable thing to do. One easy way is to make the implementation class derive from the class defined in the header.
The downside is that the compiler won't know how to construct your class, so you'll need some kind of factory method to get instances of the class. It will be impossible to have local instances on the stack.
您必须在标头中声明所有成员,以便编译器知道对象有多大等等。
但是你可以通过使用接口来解决这个问题:
ext.h:
int.h:
int.cpp:
You have to declare all members in the header so the compiler knows how large is the object and so on.
But you can solve this by using an interface:
ext.h:
int.h:
int.cpp:
最接近的答案是 PIMPL 习语。
请参阅 Herb Sutter 的快速粉刺成语。
IMO Pimpl 在开发的初始阶段非常有用,因为您的头文件将更改多次。 Pimpl 由于其在堆上分配/释放内部对象而产生成本。
The most nearest answer is PIMPL idiom.
Refer this The Fast Pimpl Idiom from Herb Sutter.
IMO Pimpl is really useful during initial stages of development where your header file is going to change many times. Pimpl has its cost due to its allocation\deallocation of internal object on heap.
查看类Handle-Body C++ 中的惯用语
Check out the class The Handle-Body Idiom in C++