首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏程序技术知识

    C语言__attribute__ ((constructor))和__attribute__ ((destructor))

    void start(void) __attribute__ ((constructor));        static        void stop(void) __attribute__ ((destructor \n"); } __attribute__((destructor)) void unload_file() { printf("destructor is called.

    2.4K10编辑于 2022-05-20
  • 来自专栏全栈程序员必看

    深入浅出MFC—Frame1[通俗易懂]

    CObject { public: CObject() { printf("CObject Constructor \n"); } ~CObject() { printf("CObject Destructor : CCmdTarget() { printf("CCmdTarget Constructor \n"); } ~CCmdTarget() { printf("CCmdTarget Destructor m_pCurrentWinApp = this; printf("CWinApp Constructor \n"); } ~CWinApp() { printf("CWinApp Destructor public CWnd { public: CView() { printf("CView Constructor \n"); } ~CView() { printf("CView Destructor //CWinApp Destructor //CWinThread Destructor //CCmdTarget Destructor //CObject Destructor 发布者:

    35110编辑于 2022-07-04
  • 来自专栏ClearSeve

    什么时候使用虚析构函数

    : Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor (){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor 输出如下: Base Constructor Called Derived constructor called Base Destructor called 我们发现派生类的析构函数并没有调用,这是有问题的 Base(){ cout << "Base Constructor Called\n"; } virtual ~Base(){ cout << "Base Destructor called\n"; } }; 输出如下: Base Constructor Called Derived Constructor called Derived destructor called

    1.1K20编辑于 2022-02-10
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.35:基类的析构函数要么是公开的虚函数,要么是保护的非虚函数

    C.35: A base class destructor should be either public and virtual, or protected and nonvirtual 基类的析构函数要么是公开的虚函数 If the destructor is public, then calling code can attempt to destroy a derived class object through a base class pointer, and the result is undefined if the base class's destructor is non-virtual. If the destructor is protected, then calling code cannot destroy through a base class pointer and the Note(注意) A destructor must be nonprivate or it will prevent using the type: 析构函数必须是非私有的,除了它不想被别人用。

    1.3K20发布于 2020-03-25
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.30:如果一个类需要明确的销毁动作,定义析构函数

    C.30: Define a destructor if a class needs an explicit action at object destruction 如果一个类需要明确的销毁动作, 定义析构函数 Reason(原因) A destructor is implicitly invoked at the end of an object's lifetime. If the default destructor is sufficient, use it. Only define a non-default destructor if a class needs to execute code that is not already part of its Example, bad(反面示例) class Foo { // bad; use the default destructor public: // ...

    50610发布于 2020-03-25
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.127:包含虚函数的类应该有虚析构函数或保护析构函数‍

    C.127: A class with a virtual function should have a virtual or protected destructor C.127:包含虚函数的类应该有虚析构函数或保护析构函数‍ Less commonly, if deletion through a pointer to base is not intended to be supported, the destructor Example, bad(反面示例) struct B { virtual int f() = 0; // ... no user-written destructor, defaults to public nonvirtual ... }; // bad: derived from a class without a virtual destructor struct D : B Flag delete of a class with a virtual function but no virtual destructor. 提示针对包含虚函数却没有虚析构函数的类的销毁操作。

    1K20发布于 2020-03-25
  • 来自专栏Reck Zhang

    C++ 08 - copy elision

    { std::cout << "default construction" << std::endl; } ~X() { std::cout << "destructor /main default construction destructor copy/move construction 都被省略. /main default construction copy constructor destructor copy constructor destructor destructor class

    43740发布于 2021-08-11
  • 来自专栏C/C++基础

    C++栈展开如何防止内存泄露

    cout<<"this is A's constructor, num="<<num<<endl; } ~A(){ cout<<"this is A's destructor } int main(){ try{ autoptrtest1(); } catch(int){ cout<<"there is no destructor endl; } cout<<endl; try{ autoptrtest2(); } catch(int){ cout<<"A's destructor does be invoked"<<endl; } } 程序的输出结果: this is A’s constructor, num=1 there is no destructor invoked this is A’s constructor, num=2 2 this is A’s destructor, num=2 A’s destructor does be invoked

    86710发布于 2018-08-03
  • 来自专栏全栈程序员必看

    c++构造函数是否可以抛出异常_什么叫抛出异常

    ; class C { int m; public: C(){ cout<<"in C constructor"<<endl;} ~C(){ cout<<"in C destructor endl;} }; class A { public: A(){ cout<<"in A constructor"<<endl;} ~A(){ cout<<"in A destructor resource=new char[100]; cout<<"in B constructor"<<endl; throw -1; } ~B() { cout<<"in B destructor cout<<"catched"<<endl; } } 程序输出结果: in A constructor in C constructor in B constructor in C destructor in A destructor catched 从输出结果可以看出,在构造函数中抛出异常,当前对象的析构函数不会被调用,如果在构造函数中分配了内存,那么会造成内存泄露,所以要格外注意。

    2.3K10编辑于 2022-09-22
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.36:析构函数不应该失败

    C.36: A destructor may not fail 析构函数不应该失败 Reason(原因) In general we do not know how to write error-free code if a destructor should fail. The writer of a destructor does not know why the destructor is called and cannot "refuse to act" by throwing Note(注意) Declare a destructor noexcept. Enforcement(实施建议) (Simple) A destructor should be declared noexcept if it could throw.

    76830发布于 2020-03-25
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.37:保证析构函数不会抛出异常

    C.37: Make destructors noexcept C.37:保证析构函数不会抛出异常 Reason(原因) A destructor may not fail. If a destructor tries to exit with an exception, it's a bad design error and the program had better terminate By explicitly marking destructors noexcept, an author guards against the destructor becoming implicitly struct X { Details x; // happens to have a throwing destructor // ... Enforcement(实施建议) (Simple) A destructor should be declarednoexceptif it could throw.

    91710发布于 2020-03-25
  • 来自专栏知识同步

    构造函数和析构函数可以是虚函数吗,在里面能调用虚函数吗

    << endl; }; ~Father() { cout << "destructor Father!" << endl; }; ~Son() { cout << "destructor Son!" << endl; }; virtual ~Father() { cout << "destructor Father!" << endl; }; ~Son() { cout << "destructor Son!" //destructor Son! //destructor Father!

    2.3K50编辑于 2022-12-26
  • 来自专栏龙进的专栏

    C++复习笔记

    args */) { cout<<"base class constructor called"<<endl; } base::~base() { cout<<"base class destructor */) { cout<<"class mid_a constructor called"<<endl; } mid_a::~mid_a() { cout<<"class mid_a destructor */) { cout<<"class mid_b constructor called"<<endl; } mid_b::~mid_b() { cout<<"class mid_b destructor called class mid_b destructor called base class destructor called class mid_a destructor called base class destructor called 分析:先尝试实例化mid_a,由于mid_a继承自base,因此调用了base的构造函数,然后调用mid_a的构造函数。

    29810编辑于 2022-10-31
  • 来自专栏Java

    面试题:基类的析构函数为何要声明为虚函数?

    Animal() { cout << "Animal constructor" << endl; } ~Animal() { cout << "Animal destructor public: Dog() { cout << "Dog constructor" << endl; } ~Dog() { cout << "Dog destructor 由于 Animal 的析构函数不是虚拟的,所以仅调用了基类的析构函数: Animal constructor Dog constructor Animal destructor 可以看到,Dog 类的析构函数没有被调用 cout << "Animal constructor" << endl; } virtual ~Animal() { cout << "Animal destructor Animal destructor 在这种情况下,派生类 Dog 的析构函数正常地被调用。

    41000编辑于 2025-01-21
  • 来自专栏用户4585225的专栏

    addOpenWithCode.reg

    () [0x7ffd94c8acb0] destructor() [0x7ffd94c8aca0] 1234567891011121314 这……没有任何区别啊,C++国际标准委员会逗我玩呢? () [0x7ffe849b8a70] moving from [0x7ffe849b8ab0] to [0x7ffe849b8aa0] destructor() [0x7ffe849b8ab0] -- () [0x7ffe849b8ab0] destructor() [0x7ffe849b8aa0] 1234567891011121314151617 可以看到在step1中调用process函数的时候 ] moving from [0x7ffe7d59f350] to [0x7ffe7d59f380] destructor() [0x7ffe7d59f350] destructor() [0x7ffe7d59f380 ] moving from [0x7ffca4276e50] to [0x7ffca4276e90] destructor() [0x7ffca4276e50] destructor() [0x7ffca4276e90

    66320发布于 2020-12-18
  • 来自专栏C/C++基础

    C++栈展开如何防止内存泄露

    cout<<"this is A's constructor, num="<<num<<endl; } ~A() { cout<<"this is A's destructor { try { uniqueptrtest1(); } catch(int) { cout<<"there is no destructor cout<<endl; try { uniqueptrtest2(); } catch(int) { cout<<"A's destructor does be invoked"<<endl; } } 程序的输出结果: this is A's constructor, num=1 there is no destructor invoked this is A's constructor, num=2 2 this is A's destructor, num=2 A's destructor does be invoked 在解读上面的这段程序的时候

    1.3K30发布于 2019-02-22
  • 来自专栏C/C++基础

    C++构造函数抛出异常注意事项

    using namespace std; class C { int m; public: C(){cout<<"in C constructor"<<endl;} ~C(){cout<<"in C destructor "<<endl;} }; class A { public: A(){cout<<"in A constructor"<<endl;} ~A(){cout<<"in A destructor"<< { resource=new char[100]; cout<<"in B constructor"<<endl; throw -1; } ~B() { cout<<"in B destructor int) { cout<<"catched"<<endl; } } 程序输出结果: in A constructor in C constructor in B constructor in C destructor in A destructor catched 从输出结果可以看出,在构造函数中抛出异常,当前对象的析构函数不会被调用,如果在构造函数中分配了内存,那么会造成内存泄露,所以要格外注意。

    2.6K40发布于 2019-02-22
  • 来自专栏宜达数字

    面向对象(八)-析构函数

    class First { ~First() { Console.WriteLine("First's destructor Second : First { ~Second() { Console.WriteLine("Second's destructor Third : Second { ~Third() { Console.WriteLine("Third's destructor Third t = new Third(); } } /* Output : Third's destructor Second's destructor is called. First's destructor is called. */

    99810发布于 2020-06-02
  • 来自专栏C++核心准则原文翻译

    C++核心准则C.31:类请求的所有资源必须在析构函数释放

    C.31: All resources acquired by a class must be released by the class's destructor 类申请的所有资源必须在析构函数释放 A destructor, close, or cleanup operation should never fail. For starters, the writer of a destructor does not know why the destructor is called and cannot "refuse Obviously, such objects should not be deleted by the class's destructor. blob/master/CppCoreGuidelines.md#c31-all-resources-acquired-by-a-class-must-be-released-by-the-classs-destructor

    71010发布于 2020-03-25
  • 来自专栏C++核心准则原文翻译

    C++核心准则​讨论:将基类的析构函数设为公共和虚拟的,或受保护的和非虚拟的

    If yes, then base's destructor must be public in order to be callable, and virtual otherwise calling If Base's destructor is public and non-virtual (the default), it can be accidentally called on a pointer For a base class destructor, therefore, the choice is between allowing it to be called via a pointer Then, even though the destructor has to be public, there can be great pressure to not make it virtual advice and give it a protected non-virtual destructor.

    1.4K20发布于 2020-12-15
领券