Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在对象超出范围之前调用析构函数_C++_Scope_Destructor - Fatal编程技术网

C++ 在对象超出范围之前调用析构函数

C++ 在对象超出范围之前调用析构函数,c++,scope,destructor,C++,Scope,Destructor,所以我一直在清理一些代码,我注意到在调用构造函数之后直接调用了类的Destructor。实际上,对象什么也不做。我很确定这个对象仍然在作用域中,因为我仍然可以访问它的成员。在构造函数中,我打印出了这个,在析构函数中,我打印出了“deleted:”listenerID); 标准::cout问题: 如果在具有自动存储持续时间的块或函数内创建对象,如 { // ... EventListener myListener(); // ... } 当执行离开块/函数时,对象将被销毁,即使它仍然

所以我一直在清理一些代码,我注意到在调用构造函数之后直接调用了类的Destructor。实际上,对象什么也不做。我很确定这个对象仍然在作用域中,因为我仍然可以访问它的成员。在构造函数中,我打印出了
这个
,在析构函数中,我打印出了
“deleted:”listenerID);
标准::cout问题:
如果在具有自动存储持续时间的块或函数内创建对象,如

{
  // ...
  EventListener myListener();
  // ...
}
当执行离开块/函数时,对象将被销毁,即使它仍然可以从其他地方引用。另见:

一般来说,您不应该将指针传递到具有此类作用域的对象的任何内部存储位置

解决方案: 如果希望对象位于当前块之外,则必须显式使用
new

{
  // ...
  EventListener* myListener = new EventListener();
  // ...
}

我很确定这个对象仍然在范围内,因为我可以访问它的 各位议员仍然支持


注意:即使对象被(隐式)销毁,指向该对象的指针似乎仍然可用,但取消引用该指针是一个严重的错误,尽管并不总是明显的错误。

除了厨房水槽之外,您已经包含了所有内容,创建
EventListener
/
场景的代码在哪里?@user657267在场景的构造函数中。如果您的意思是声明,那么它也在场景中。如何创建场景?您没有使用函数范围创建EventListener?在这种情况下,只要函数返回,对象就会被销毁,即使它仍然可以从其他地方引用(它本来不应该被引用)。另请参见:@BlueSpud-另外,似乎更好的设计是使用
std::map
,或者更好的设计是使用
std::map
而不是std::vector来容纳所有侦听器。您编写的代码中有一半不需要编写。
//This will recive events
class EventListener
    {
        //this is what type of event it will repsond to
    public:
        EventTypes typeEvent;
        EventListener(EventTypes typeEvent, EventFunction);
        EventListener();
        ~EventListener();
        EventFunction functionPointer;
        void removeListener();
    private:
        int listenerID;
};

//her we define the event dispatcher
class EventDispatcher
    {
    public:
        int addEventListener(EventListener*);
        void removeEventListener(int);
        void dispatchEvent(EventTypes, Event*);

    private:
        std::vector<EventListener*>EventListeners;
    };
class Scene
    {
        public:

            Scene();

            std::vector<StaticGeometry>GameObjects;
            void addStaticGeometry(StaticGeometry object);
            void renderSceneWithCamera(camera cam);
            void renderSceneWithCameraAndProgram(camera cam,GLuint program);

            void pickObjectFromScene();
            void pickObjectFromSceneWithScreenCoords(int x, int y);
            int selectedObject;


        private:
                //listen for the left click
                EventListener leftClickEventListener;
                void leftClick(Event* eventPtr);

    };

Scene::Scene() : leftClickEventListener(EventTypeLeftMouseClick,std::bind(&Scene::leftClick,this,std::placeholders::_1))
    {
        //default constructor, we just need to make sure that the selected thing is -1
        selectedObject = -1;
    }
{
  // ...
  EventListener myListener();
  // ...
}
{
  // ...
  EventListener* myListener = new EventListener();
  // ...
}