委托构造函数C ++ [英] Delegate Constructor C++
本文介绍了委托构造函数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这样做吗?我试图委托一个C ++类构造函数,因为它基本上是相同的代码重复3次。我读了C ++ x11和阅读g ++ 4.7.2允许这个,但我不知道如果我做的
Am I doing this right? I'm trying to delegate a C++ class constructor as it's basically the same code repeating 3 times.. I read up on C++x11 and read that g++ 4.7.2 allows this but I'm not sure if I'm doing it right:
Bitmap::Bitmap(HBITMAP Bmp)
{
//Construct some bitmap stuff..
}
Bitmap::Bitmap(WORD ResourceID)
{
HBITMAP BMP = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ResourceID), IMAGE_BITMAP, 0, 0, LR_SHARED);
Bitmap(BMP); //Delegates to the above constructor? Or does this create a temporary?
}
或者我需要做:
Bitmap::Bitmap(HBITMAP Bmp)
{
//Construct some bitmap stuff..
}
Bitmap::Bitmap(WORD ResourceID) : Bitmap((HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ResourceID), IMAGE_BITMAP, 0, 0, LR_SHARED))
{
}
推荐答案
委托构造函数只能在构造函数的初始化列表中工作,否则你只会创建一个临时的或者像你提到的其他错误。
You need to do the second. Delegating constructors only works in the constructor's initialization list, otherwise you'll just create a temporary or do other mistakes like you mentioned.
这篇关于委托构造函数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文