[英]Error While creating a class whose object cant be created on stack but only on Heap?
I tried writing a code to create a class, whose object can only be created on heap but not stack. 我尝试编写代码来创建一个类,该类的对象只能在堆上创建,而不能在堆栈上创建。 But During Compilation I got some linking error. 但是在编译期间我遇到了一些链接错误。
# include<iostream>
# include<stdio.h>
# include<conio.h>
using namespace std;
class Rect
{
int length;
int breadth;
Rect();
public :
Rect & operator = (const Rect&);
Rect(const Rect& abc)
{
cout<<"in copy const"<<"\n";
}
~Rect();
int area_rect()
{
return length*breadth;
}
void set_value(int a,int b);
static Rect* instance()
{
Rect* ptr=NULL;
ptr=new Rect ;
return ptr;
}
};
void Rect::set_value(int a,int b)
{
length=a;
breadth=b;
}
int main()
{
Rect* a= Rect::instance();
a->set_value(10,3);
cout << "area realted to object a : " << a->area_rect() <<"\n";
Rect* b=a;
b->set_value(10,4);
cout << "area realted to object a : " << a->area_rect() <<"\n";
cout << "area realted to object b : " << b->area_rect() <<"\n";
delete b;
getch();
return 0;
}
I got the following error: 我收到以下错误:
ccUfbaaa.o(.text+0x24f) In function `main': [Linker error] undefined reference to `Rect::~Rect()'
ccUfbaaa.o(.text$_ZN4Rect8instanceEv[Rect::instance()]+0x60) In function `ZN4Rect9area_rectEv': [Linker error] undefined reference to `Rect::Rect()'
ccUfbaaa.o(.text$_ZN4Rect8instanceEv[Rect::instance()]+0x60) ld returned 1 exit status .
I know I can make destructor private that Will allow object to be crated only on heap . 我知道我可以将析构函数设为私有,这将允许仅在堆上创建对象。 But in that case how can i delete the created object ? 但是在那种情况下,如何删除创建的对象? And How can we correct this above error? 我们如何纠正上述错误?
You just have to implement the default constructor and the destructor somewhere: 您只需要在某个地方实现默认的构造函数和析构函数即可:
Rect::Rect():length(0),breadth(0) {};
Rect::~Rect() {};
and then, making public Rect::instance()
, Rect::set_value(int, int)
and Rect::area_rect()
, will help 然后公开Rect::instance()
, Rect::set_value(int, int)
和Rect::area_rect()
会有所帮助
The way you can enforce that your type can only be put on the heap but not on the heap is have a base class with a pure virtual destructor base and to make the destructr private: since the destructor isn't accessible to the full object, it can't be put on to the stack but it can be deleted through a pointer to the base. 强制您的类型只能放在堆上而不能放在堆上的方法是,使用具有纯虚拟析构函数基的基类,并将析构函数设为私有:由于析构函数不可用于整个对象,它不能放在堆栈上,但是可以通过指向基址的指针删除。 To prevent a derived class can be put on the stack (if this should be prevented, too) you want to make the type final
(which is only available in C++2011, though): 为了防止将派生类放到堆栈上(如果也应避免这种情况),您需要将类型设置为final
(尽管仅在C ++ 2011中可用):
struct A { virtual ~A() = 0; };
A::~A() {}
struct B final: A { private: ~B() {} };
int main()
{
//B berror; // ERROR: destructor not accessible
B* b = new B;
delete static_cast<A*>(b);
}
The fact some some of your members were declared but not defined was already mentioned in other answers to this questions. 您的某些成员已被声明但未定义的事实已在此问题的其他答案中提及。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.