C++ 对于Singleton类,我应该如何编写一个复制构造函数?对于同一个类,我应该如何重载=运算符?
我应该如何为我的singleton类编写一个复制构造函数,以防止创建一个新对象,因为我已经有了一个新对象。对于相同的问题,重载=运算符的最佳做法是什么C++ 对于Singleton类,我应该如何编写一个复制构造函数?对于同一个类,我应该如何重载=运算符?,c++,singleton,copy-constructor,C++,Singleton,Copy Constructor,我应该如何为我的singleton类编写一个复制构造函数,以防止创建一个新对象,因为我已经有了一个新对象。对于相同的问题,重载=运算符的最佳做法是什么 #include <iostream> #include <stdio.h> #include <conio.h> using namespace std; class Rect { int length; int breadth; static int count; stat
#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;
}
#包括
#包括
#包括
使用名称空间std;
类矩形
{
整数长度;
整数宽度;
静态整数计数;
静态整数最大计数;
Rect()
{};
Rect(constrect&abc){};
公众:
~Rect();
int area_rect()
{返回长度*宽度;}
void set_值(int a,int b);
静态Rect*实例()
{
Rect*ptr=NULL;
如果(计数<最大计数)
{
ptr=新的Rect;
计数++;
}
返回ptr;
}
};
int Rect::count=0;
int Rect::maxcount=1;
void Rect::set_值(int a,int b)
{
长度=a;
宽度=b;
}
Rect::~Rect()
{
计数--;
}
int main()
{
Rect*a=Rect::instance();//创建第一个对象
//Rect*c=Rect::instance();//无法创建第二个对象
//当maxcount=1并返回NULL时
a->设置_值(10,3);
你能不能像这里解释的那样让它不可复制
或者定义复制构造函数和赋值运算符,以获得相同的单例。具体取决于实际需要的功能
通常也应禁止分配(如上链接所示):
这也禁止移动操作
您可能还想知道:
单例是荒谬的,只需使用免费函数即可
不过,要回答你的问题
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 &);
};
这里有一个相关的答案:如果你不想有人创建一个对象,不要给他们公共构造函数
说真的,你可以拥有一个工厂对象,使它成为你想要限制其构造的类的朋友,并使该类的构造函数成为私有的。这样,访问一个对象(此时甚至不必创建它)的唯一方法就是通过你的工厂
e、 g
#包括
#包括
类连接工厂;
类数据库连接:boost::noncopyable{
//只能由发生在的ConnectionFactory创建
//知道如何连接到我们的数据库服务器!
友邦级连接工厂;
数据库连接(std::string用户名、std::string密码/*等*/);
//不希望任何随机用户重置连接!
~DatabaseConnection();
公众:
//公共接口位
无效执行(const Select&);
// ...
};
连接工厂{
公众:
数据库连接&conn();
};
类可能需要数据库连接{
连接工厂;
公众:
显式MayNeedDbConnection(const ConnectionFactory&f):工厂(f)
{}
void SelectStuff(){
DatabaseConnection&conn=factory.conn();
连接执行(…);
}
};
int main()
{
连接工厂;
可能需要连接md(工厂);
md.SelectStuff();
}
我认为“最佳实践”是根本没有一个单一对象。如果你不想拥有多个对象,就不要制作多个。@Ritesh:重新思考和重新设计。如果其他人制作了一个对象,为什么会有问题?只要告诉他们使用你现有的对象。只拥有一个对象是有区别的(完全好的,考虑使用一个全局变量)并强制只构建一个。@ KerrekSB,SuntLon的整个概念更像是胡扯?”瑞特斯:是的,单身人士是牛屎。还有,Ritesh,正如你所解释的,你基本上想阻止你的同事错误地复制,所以这很好。
class Foo {
private:
// Don't write bodies.
Foo (const Foo &);
Foo & operator = (const Foo &);
};
#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();
}