C++, preventing class instance from being created on the stack (during compiltaion)

前端 未结 5 714
谎友^
谎友^ 2020-12-29 00:29

I know there are methods to prevent a class from being created on the heap, by preventing the user from using the new and delete operator. I am try

相关标签:
5条回答
  • 2020-12-29 00:41

    Whenever a local variable goes out of scope, it is destroyed. And on destruction, destructor of object is called. Here, scope is of main function. When program exits, myclass object's destructor will be called

    0 讨论(0)
  • 2020-12-29 00:46

    Because when instance goes out of scope, it has to be destructed using destructor. Pointer to instance does not do this.

    0 讨论(0)
  • 2020-12-29 00:52

    Why would the destructor be called while creating an instance of MyClass?

    It isn't. It must be invoked automatically when the instance goes out of scope, though. If it's private, the compiler must not generate that code, hence the error.

    If you think making the destructor private is obscure, another way to restrict a class to dynamic allocation is to make all the constructors private and only have MyClass::create() functions returning dynamically allocated objects:

    class MyClass {
    public:
      static MyClass* create()             {return new MyClass();}
      static MyClass* create(const Foo& f) {return new MyClass(f);}
    private:
      MyClass();
      MyClass(const Foo&);
    };
    

    Note that returning naked pointers to objects that must be deleted is frowned upon. You should return smart pointers instead:

    class MyClass {
    public:
      static std::shared_ptr<MyClass> create()             {return new MyClass();}
      static std::shared_ptr<MyClass> create(const Foo& f) {return new MyClass(f);}
      // ...
    };
    
    0 讨论(0)
  • 2020-12-29 00:57

    It isn't. The compiler is trying to call the destructor when it goes out of scope, and is indicating to the line of code that produces this effect, which is much more useful than pointing at the end of the function.

    0 讨论(0)
  • 2020-12-29 01:05

    When myclass reaches the end of its scope (the next }) the compiler calls the destructor to free it from the stack. If the destructor is private, however, then the destructor cannot be accessed, so the class cannot be placed on the stack.

    I don't like the look of delete this. In general I think objects should not destroy themselves. Perhaps a better way is to have a private constructor for your class then use a static function to create an instance.

    // In class declaration...
    static MyClass* Create()
    {
        return new MyClass(); // can access private constructor
    }
    
    // ...
    
    MyClass myclass; // illegal, cannot access private constructor
    
    MyClass* pMyClass = MyClass::Create();
    delete pMyClass; // after usage
    
    0 讨论(0)
提交回复
热议问题