如果是 Singleton 类,我应该如何编写复制构造函数,以及如何重载 = 运算符?
我应该如何为我的单例类编写一个复制构造函数,以防止创建新对象,因为我已经有了一个。重载=运算符的最佳实践是什么
#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;
}
如何编写复制构造函数代码并重载等于运算符以防止创建更多对象?
How should I write a copy constructor for my singleton class to prevent the creation of a new object as I already have one . And what is the best practice to overload = operator for same
#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;
}
How to write copy constructor code and overloading equal operator for prevention of creation of further object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要么按照此处的说明将其设置为不可复制
我该如何制作这个 C++ 对象不可复制?
或者您定义复制构造函数和赋值运算符,以便获得相同的单例。根据您实际想要的功能。
通常也应该禁止分配(如上面的链接所示):
这也禁止移动操作。
您可能还想了解这一点:
单例真的那么糟糕吗?
Either you make it non-copyable as explained here
How do I make this C++ object non-copyable?
or you define the copy constructor and assignment operators such that you get the same singleton. Depending on the functionality you actually want.
Assignment should typically also be forbidden (as on the link above):
This also forbids move operations.
You may also want to know this:
Are Singletons really that bad?
单例是荒谬的,只使用免费函数。
不过,回答你的问题......
C++11:
C++03;
Singletons are ridiculous, just use free functions.
Still, to answer your question...
C++11:
C++03;
这是相关的答案:如果您不希望某人创建对象,请不要给他们公共构造函数!
认真地说,您可以拥有一个工厂对象,使其成为您想要限制其构造的类的朋友,并将该类的构造函数设为私有。这样,访问对象(甚至不必在此时创建它)的唯一方法就是通过工厂。
例如
Here is related answer : if you don't want someone to create an object, don't give them public constructor!
Seriously, you can have a factory object, make it a friend to class which you want to restrict construction of, and make constructor of that class private. This way the only way to access an object (without even necessarily creating it at this time) would be through your factory.
e.g.