简体   繁体   English

错误当创建一个不能在堆栈上只能在堆上创建其对象的类时?

[英]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.

相关问题 堆栈展开动态创建的对象,其构造函数也作用于堆 - Stack unwinding dynamically created object whose constructor acts also on heap 在堆栈/堆上创建对象 - creating object on stack/heap 堆栈溢出错误但无法在堆上分配 - stack overflow error but cant allocate on the heap 如何在1&gt;仅堆栈而不是堆上和2&gt;仅堆栈不堆上创建对象 - how to create object on 1>stack only and not on heap and 2>heap only not on stack 在函数中创建的对象,是保存在堆栈还是堆上? - object created in function, is it saved on stack or on heap? 在仅具有参数化的Ctor的另一个类中创建一个类的对象时,“错误:x不是类型” - while creating object of a class in another class having only parameterized Ctor “error: x is not a type” 在堆或堆栈上创建的类的本地变量是否未声明为指针? - Are variables local to a class that are not declared as pointers, created on the heap or stack? 在C ++中可以将在堆上创建的对象视为“堆栈中”吗? - Could be an object created on the heap treated as 'being on stack' in C++? 与在堆上创建一个小对象(一次)相比,在堆栈上分配一个小对象是否更有效? - Is it more efficent to allocate a small object on the stack as apposed to creating it on the heap (once)? 为什么不能创建空类的 const 对象 - why a const object of an empty class cant be created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM