2012-01-16 13 views
2

シングルトンクラスのコピーコンストラクタを作成して、新しいオブジェクトの作成を防止するにはどうすればよいですか?そして、コピーコンストラクタコードを記述し、さらにオブジェクトの作成のためprvention等しい演算子をオーバーロードする方法と同じシングルトンクラスの場合にはコピーコンストラクタをどのように書くべきですか?

# include<iostream> 
# include<stdio.h> 
# include<conio.h> 

using namespace std; 

class Rect 
{ 
    int length; 
    int breadth; 
    static int count; 
    static int maxcount; 
    Rect() 
    {}; 
    Rect(const Rect& abc){}; 
    public : 

     ~Rect(); 
     int area_rect()   
     {return length*breadth;} 
     void set_value(int a,int b); 

     static Rect* instance() 
     {  
      Rect* ptr=NULL; 
      if(count < maxcount) 
      { 
       ptr=new Rect ; 
       count++; 
      } 
      return ptr; 
     } 
    }; 
    int Rect::count = 0; 
    int Rect::maxcount = 1; 
    void Rect::set_value(int a,int b) 
    { 
    length=a; 
    breadth=b; 
    } 
    Rect::~Rect() 
    { 
    count --;   
    } 
int main() 
{ 
    Rect* a= Rect::instance(); //creates first object 
    // Rect* c= Rect::instance(); //fails to create second object 
    //as maxcount=1 and returns NULL 
    a->set_value(10,3); 
cout << "area realted to object a : " << a->area_rect() <<"\n"; 
Rect* b=a;//allows creation of second object which i dont want 
b->set_value(10,4); 
cout << "area realted to object b : " << b->area_rect() <<"\n"; 
delete a; 
delete b; 
getch(); 
return 0; 
}  

ために=演算子をオーバーロードするためのベストプラクティスは何ですか?

+5

「ベストプラクティス」は、シングルトンを全く持たないことだと思います。あなたが1つ以上持っていたくない場合は、1つ以上を作ってはいけません。 –

+0

@KerrekSB他の誰かが同じアプリケーションで動いていて、それを初めて使っていて、別のオブジェクトを作って、それが起こりたくないのですか? – Invictus

+1

Rect * b = a; 2番目のオブジェクトを作成しません。 "b"は "a"へのポインタになるでしょう – Neox

答えて

10

ここ

How do I make this C++ object non-copyable?

またはあなたが同じシングルトンを取得するようなコピーコンストラクタと代入演算子を定義を説明するように、あなたはそれが非コピー可能にするのどちらか。実際に必要な機能に応じて。

割り当ては、典型的には、(上記のリンク上のように)禁止されるべきである:

class Rect{ 
    Rect(const Rect&) = delete; 
    Rect& operator=(const Rect&) = delete; 
    . . . 
} 

これはまた、移動操作を禁止します。

ます。また、このことを知ってほしいことがあります。 Are Singletons really that bad?

+0

Riteshは、基本的にあなたの同僚が誤ってコピーするのを防ぐために説明しました。 –

+0

クイックヘルプのあなたに+1 ...よろしくご助けください – Invictus

7

シングルトンはちょうど無料の機能を使用し、ばかげています。

それでも

、あなたの質問に答えるために...

C++ 11:

class Foo { 
public: 
    Foo (const Foo &) = delete; 
    Foo & operator = (const Foo &) = delete; 
}; 

C++ 03。

class Foo { 
private: 
    // Don't write bodies. 
    Foo (const Foo &); 
    Foo & operator = (const Foo &); 
}; 
+0

あなたに迅速な助けを...よろしく – Invictus

0

これは関連する回答です:誰かがオブジェクトを作成しないようにするには、パブリックコンストラクタを与えないでください!

真剣に、ファクトリオブジェクトを作成し、それを作成するクラスを友人にして、そのクラスのコンストラクタをプライベートにすることができます。このようにして、オブジェクトにアクセスする唯一の方法(この時点で作成する必要はありません)は、あなたの工場を経由します。

#include <boost/noncopyable.hpp> 
#include <string> 

class ConnectionFactory; 

class DatabaseConnection : boost::noncopyable { 
    // can only be created by ConnectionFactory which happens to 
    // know how to connect to our database server! 
    friend class ConnectionFactory; 
    DatabaseConnection(std::string username, std::string password /*etc*/); 
    // don't want any random user to reset the connection! 
    ~DatabaseConnection(); 

public: 
    // public interface bits 
    void Execute(const Select&); 
    // ... 
}; 

ConnectionFactory { 
public: 
    DatabaseConnection& conn(); 
}; 

class MayNeedDbConnection { 
    ConnectionFactory factory; 
public: 
    explicit MayNeedDbConnection(const ConnectionFactory& f) : factory(f) 
    {} 

    void SelectStuff() { 
    DatabaseConnection& conn = factory.conn(); 
    conn.Execute(....); 
    } 
}; 

int main() 
{ 
    ConnectionFactory factory; 
    MayNeedDbConnection md(factory); 
    md.SelectStuff(); 
} 
関連する問題