C:破坏静态东西的最佳方法

C:破坏静态东西的最佳方法,第1张

概述当我有一个包含静态内容的类时,如何在应用程序结束时以最佳方式释放内存? foo.h中 class GLUtesselator;class Foo{private: static GLUtesselator *tess;public: Foo(); virtual ~Foo();} Foo.cpp中 #include "Foo.h"#include <GL/g 当我有一个包含静态内容的类时,如何在应用程序结束时以最佳方式释放内存?

foo.h中

class glutesselator;class Foo{private:    static glutesselator *tess;public:    Foo();    virtual ~Foo();}

Foo.cpp中

#include "Foo.h"#include <GL/glu.h>glutesselator *Foo::tess = gluNewTess(); // System callFoo::Foo() {}Foo::~Foo(){     // And of course I don't want to destruct it here,// because I'm going to use the tesselator in other instances of Foo     // Otherwise:     // gluDeleteTess(tess);}

是否有更好的替代方法来制作一个方法来删除静态的东西,并在应用程序终止时调用它?
或者我可以说:“哦,无论如何,应用程序终止. *** 作系统将释放内存……”?

谢谢

解决方法 简单.不要使静态成员成为指针.
然后它将被正确构造和破坏.

foo.h中

#include <GL/glu.h>class Foo{    private:        static glutesselator  tess;    public:                 Foo();        virtual ~Foo();};

Foo.cpp中

// glutesselator  Foo::tess;

如果你必须使用gluNewTess()和gluDeleteTess(),那么你可以使用共享指针.我没有编译器,所以确切的用法可能不是绝对正确的.但是shared_ptr确实具有这种能力.

foo.h中

#include <GL/glu.h>typedef std::shared_ptr<glutesselator,voID (*)(glutesselator*)> autogluTess;class Foo{    private:        static autogluTess  tess;    public:                 Foo();        virtual ~Foo();};

Foo.cpp中

// autogluTess    Foo::tess(gluNewTess(),&gluDeleteTess);
总结

以上是内存溢出为你收集整理的C:破坏静态东西的最佳方法全部内容,希望文章能够帮你解决C:破坏静态东西的最佳方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1244415.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存