在从POD结构继承的类中定义复制构造函数 [英] Defining copy constructor in a class inherited from POD struct

查看:72
本文介绍了在从POD结构继承的类中定义复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道编译器为POD结构定义了默认构造函数,复制构造函数,赋值运算符和析构函数(如果未手动定义).通常(或者也许我应该说总是)这是一个复制操作.因此,我决定从Win结构BITMAP继承我的类,以在构造函数中提供内存分配,并在析构函数中释放内存.我没有使用过我想允许与某些WinAPI函数一起使用的composition'coz.这是一些代码:

As you know compiler defines default constructor, copy constructor, assignment operator and destructor for POD structures if it weren't defined manually. Usually (or maybe should I say always) it's a bit copy operation. So I've decided to inherit my class from Win structure BITMAP to provide memory allocation in constructor and freeing in destructor. I haven't used composition 'coz I want to allow using it with some WinAPI functions. Here is a some piece of code:

class CPreviewFrame : public BITMAP
{
public:
   CPreviewFrame( );
   CPreviewFrame( std::size_t width, std::size_t height, UCHAR bytesPerPixel = 3 );
   CPreviewFrame( const CPreviewFrame& frame );

   ~CPreviewFrame( );
   .....
};

复制构造函数定义如下:

And copy constructor is defined something like that:

CPreviewFrame::CPreviewFrame( const CPreviewFrame& frame ):
   BITMAP( static_cast<BITMAP>(frame) ), //question is here
   m_bufferSize( frame.m_bufferSize )
{
   bmBits = new uint8_t[ m_bufferSize ];
   memcpy( bmBits, frame.bmBits, m_bufferSize );
}

所以我的问题是:从继承的结构调用编译器定义的复制构造函数是正确的方法还是应该在构造函数主体中手动复制所有字段?这两种变体对我来说都有些奇怪,因为尽管编译器定义了POD结构,但POD结构却无法使用.如果按定义不存在,如何调用POD数据类型的构造函数?

So my question is: Is it proper way to call compiler defined copy constructor from inherited structure or should I copy all fields manually in constructor body? Both variants look somewhat strange for me because POD structs can't have constructors despite compiler defines them. How can you call constructor for POD data type if it doesn't exist by definition?

P.S.上面提到的代码在VS2010上可以很好地编译.

P.S. Code mentioned above compiles well on VS2010.

P.P.S.我发布的与此主题相关的问题

P.P.S. There is related question to this theme posted by me here.

推荐答案

BITMAP(frame)将完​​成此技巧而无需强制转换,因为编译器将知道父代的类型并且能够隐式转换参数.请注意,如果要使用强制类型转换来显式显示您的工作,请将强制类型转换为引用类型: BITMAP(static_cast< const BITMAP&>(frame)),

BITMAP(frame) will do the trick with no cast needed as the compiler will know the type of the parent and be able to implicitly convert the argument. Note that if you want to use a cast to explicit show what you're doing, cast as a reference type: BITMAP( static_cast<const BITMAP&>(frame) ),

此外,请认真考虑此继承.从现在开始的一两年左右,某人将删除您的 CPreviewFrame 对象中的一个作为 BITMAP .最好的情况是它们泄漏内存,最坏的情况是您花费数天或数周的时间调试应用程序无法正常运行的原因.组合(具有适当的接口,可以在后台调用WinAPI函数)通常并没有那么复杂,并且可以在此处更准确,更正确地为您的问题建模.

Also, seriously consider this inheritance. Sometime a year or two from now someone's going to delete one of your CPreviewFrame objects as a BITMAP. Best case they leak memory, worst case you spend days or weeks debugging why the app isn't working right. Composition (with an appropriate interface that may call WinAPI function behinds the scene) isn't typically that complicated to implement and it will more accurately and correctly model your problem here.

或者,您可以使用composition,仅向位图部分提供吸气剂(如注释中所建议).这样可以显示您的类的实现细节,但在短期内可以使编写Win API的代码更容易.

Alternately you could use composition and just provide a getter to the bitmap portion (as suggested in a comment). This exposes implementation details of your class but might make it easier to code to the Win API in the short term.

这篇关于在从POD结构继承的类中定义复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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