本文介紹了您如何為成員使用非默認構造函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有兩個班
class a {
public:
a(int i);
};
class b {
public:
b(); //Gives me an error here, because it tries to find constructor a::a()
a aInstance;
}
我怎樣才能讓 aInstance
用 a(int i)
實例化,而不是嘗試搜索默認構造函數?基本上,我想從 b
的構造函數中控制 a
的構造函數的調用.
How can I get it so that aInstance
is instantiated with a(int i)
instead of trying to search for a default constructor? Basically, I want to control the calling of a
's constructor from within b
's constructor.
推薦答案
你需要在構造函數初始化列表中顯式調用a(int):
You need to call a(int) explicitly in the constructor initializer list:
b() : aInstance(3) {}
其中 3 是您要使用的初始值.雖然它可以是任何整數.有關訂單的重要說明和其他注意事項,請參閱評論.
Where 3 is the initial value you'd like to use. Though it could be any int. See comments for important notes on order and other caveats.
這篇關于您如何為成員使用非默認構造函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!