2017-03-24 3 views
0

私は、オブジェクトの作成中に呼び出す必要があるパラメータ化されたコンストラクタで構成されるクラスを持っています。このクラスには、プライベートコピーコンストラクタも含まれています。次に、このクラスのparamterコンストラクタを呼び出す方法を示します。クラスへのポインタ参照を作成できると思います。しかし、参照を使用してパラメータコンストラクタを呼び出す方法は?コピーコンストラクタを含むクラスのパラメータコンストラクタをプライベートとして呼び出すにはどうすればよいですか?

私のプログラム:

#include<iostream> 
#include<string> 
using namespace std; 

class ABase 
{ 
protected: 
    ABase(string str) { 
     cout<<str<<endl; 
     cout<<"ABase Constructor"<<endl; 
    } 
    ~ABase() { 
    cout<<"ABASE Destructor"<<endl; 
    } 
private: 
    ABase(const ABase&); 
    const ABase& operator=(const ABase&); 
}; 


int main(void) 
{ 
    ABase *ab;//---------How to call the parameter constructor using this?? 

    return 0; 
} 

答えて

1

あなたが必要とする構文は、例えば、foostd::stringインスタンスまたはstd::stringは、リテラルconst char[]として、建設を取ることができる何かのいずれかであるABase *ab = new ABase(foo);です"Hello"

deleteに電話してメモリを解放することを忘れないでください。

(あなたはポインタ型を必要としない場合は別の方法として、あなたがABase ab(foo)を書くことができます。)

+0

※私は 'const auto ab = std :: make_unique (foo);' :: unique_ptrを作成し、 'make_unique'が' new'を非表示にします)。 –

関連する問題