简体   繁体   English

在C ++中声明类内对象的最佳实践是什么?

[英]What is the best practice in C++ to declare objects within classes?

I'm working on a class library in VC++ that shall have ref classes and some native code. 我正在VC ++中的类库上工作,该类库应具有ref类和一些本机代码。 Now, I need to use five native objects from an external framework. 现在,我需要使用来自外部框架的五个本机对象。 those objects must be declared on the heap NOT on the stack. 这些对象必须在堆上声明,而不是在栈上声明。 I want encapsulate those objects in some sort of a structure knowing that constructors need to be called. 我想将这些对象封装在某种结构中,因为知道需要调用构造函数。

Thank you. 谢谢。

Well, for each unmanaged class X, declare a ref class XWrapper that stores the unmanaged pointer to X privately, new's in constructor, and delete's in destructor. 好了,对于每个非托管类X,声明一个ref类XWrapper,该类专门存储指向X的非托管指针,在构造函数中为new,而在析构函数中为Delete。 Why not? 为什么不?

You should wrap a shared_ptr like this: 您应该像这样包装shared_ptr

template <class T>
class my_object_wrapper
{
  boost::shared_ptr<T> ptr; // or std::tr1::shared_ptr<T> if available
public:
   my_object_wrapper()
   : ptr(new T)
   {
   }

   boost::shared_ptr<T> ptr() const { return ptr; }
}

You could also use the pheonix approach: 您还可以使用pheonix方法:

template <class T>
class my_object_wrapper
{
  boost::shared_ptr<T> ptr; // or std::tr1::shared_ptr<T> if available
public:
   my_object_wrapper()
   {
   }

   boost::shared_ptr<T> ptr() 
   { 
     if (ptr == null)
       ptr = new T;

     return ptr;
   }
}

I'm not sure I know exactly what you're asking. 我不确定我确切知道你在问什么。 Constructors always need to be called on objects whether they are on the heap or the stack. 无论对象是在堆上还是在堆栈上,都始终需要在它们上调用构造函数。 If you meant that you want something to automatically call destructors for heap allocated memory, then you can use either std::auto_ptr or boost::shared_ptr . 如果您想让某些东西自动为堆分配的内存调用析构函数,则可以使用std :: auto_ptrboost :: shared_ptr Note that these are not equivalent so read the documentation! 请注意,这些不是等效的,因此请阅读文档! Auto pointers cannot be used in standard containers as they do not have the copy semantics necessary, while boost::shared_ptr can as it counts references as it copies. 自动指针不能在标准容器中使用,因为它们没有必需的复制语义,而boost :: shared_ptr可以在复制时对引用进行计数。

To answer your more general question of declaration best-practices, you want to only fully declare objects that you need to and forward declare when you can. 为了回答更常见的声明最佳实践问题,您只想完全声明所需的对象,并在可能时转发声明。 For example, if you have a class like: 例如,如果您有一个类似的类:

// In X.h
class MyConfig;
class X
{
    X(const MyConfig &config);

private:
    const MyConfig &config;
};

// In X.cpp
#include "MyConfig.h"
X::X(const MyConfig &config) : config(config)
{

}

Here you do not need to full declaration contained in MyConfig.h within the Xh header file. 在这里,您不需要在Xh头文件的MyConfig.h中包含完整的声明。 You can do this b/c you do not need to know the size of the MyConfig object to construct class X as it only contains a reference which is the same size regardless of what the underlying object is. 您可以这样做,因为您不需要知道MyConfig对象的大小即可构造类X,因为它仅包含一个相同大小的引用,而不管基础对象是什么。 Doing this will help with dependencies and will also reduce compile times. 这样做将有助于解决依赖性问题,并减少编译时间。

If on the other hand the private member config was changed to const MyConfig x; 另一方面,如果私有成员配置已更改为const MyConfig x; then you would have to include the MyConfig.h header in Xh because to construct class X requires knowledge of how much memory to allocate to store a MyConfig object. 那么您将不得不在Xh中包含MyConfig.h标头,因为构造类X需要了解分配多少内存来存储MyConfig对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM