![](/img/trans.png)
[英]Crash in constructor with using virtual inheritance and delegating constructors
[英]Delegating constructors: an initializer for a delegating constructor must appear alone
我有一對在C ++ 03風格下運行良好的構造函數。 其中一個構造函數調用超類(或基類)構造函數......
class Window : public Rectangle
{
public:
Window() : win(new RawWindow(*this))
{
refresh();
}
Window(Rectangle _rect) : Rectangle(_rect), win(new RawWindow(*this))
{
refresh();
}
...
我試圖弄清楚如何使用新的C ++ 11委托ctor功能來解決這個問題。 但是,以下代碼給出以下編譯器錯誤...
class Window : public Rectangle
{
public:
Window() : win(new RawWindow(*this))
{
refresh();
}
Window(Rectangle _rect) : Rectangle(_rect), Window(){}
“委托構造函數的初始化程序必須單獨出現”......
有沒有辦法解決??
問題是Rectangle
被初始化了兩次。
您可以嘗試更改哪一個代表什么:
Window(Rectangle _rect) : Rectangle(_rect), win(new RawWindow(*this))
{
refresh();
}
Window() : Window(Rectangle()) {}
最好的解決方案可能是避免在此示例中委托構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.