=delete,=default

  • [= delete]

using private constructor:

application-1:

点击查看代码
#include<iostream>

using namespace std;

class B;
class A {
friend class B;
private:
    A() {};
public:
    void print() {
        cout << " i am an object of class A"
        << endl;
    }

};
class B {
public:
    void print() {
        //note: declared private here
        A().print();
    }
};


int main() {
    //error:A::A()' is private within this contex
    //A a;
    B().print();
    return 0;
}

application-2:sigleton

  • [= default]
posted @ 2022-07-24 18:35  locker_10086  阅读(14)  评论(0编辑  收藏  举报