c++

The other way to use con/destructor

  • Classes that unallowed to Copy

    If you wanna simulate a country and you can just get a president, and President class will be in danger!

    1
    2
    3
    4
    President ourPresident;
    DoSomething(ourPresident);//duplicate created in passing by value
    President clone;
    clone = ourPresident; //duplicate via assignmant

    Obviously, you should avoid this situation.

    To ban the copy of class, you can declare a Privative Copy constructor, which make sure the ‘DoSomething(OurPresident)’ can’t pass the compile.

    To ban assignment, you can declare a Privative assignment operator.

    So, the follow can solve:

    1
    2
    3
    4
    5
    6
    7
    class President
    {
    private:
    President(const President&);
    President & operator = (const President&);

    }

    Above but has a shortage: You can’t instantiatiate multiple objects to create multiple presidents.

    1
    President One,Two,Three;

    owing to the privative copy constructor, every object is not copyable,buy ours goal is make sure President class has and only has one embodiment.

    Once you got one President object, creating others President objects are banned.

    Then you need ‘Static’

  • Used for class’ data members, it will be shared in all instance.

  • For local variable in a func, the variable will not change between twice call

  • For member funcs(methods), the method is shared in all member funcs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<string>
using namespace std;

class President
{
private:
President() {}; //default constructor
President(const President&);//copy constructor
const President& operator=(const President&);//assignment operator

string name;

public:
static President& GetInstance()
{
static President onlyInstance;
return onlyInstance;
}

string GetName()
{
return name;
}

void SetName(string InputName)
{
name = InputName;
}

};

int main()
{
President& onlyPresident = President::GetInstance;
onlyPresident.SetName('JackHvikee');

//President second; //cannot access constructor
//President* third = new President();//cannot access constructor
//President fourth = onlyInstance;//cannot access copy constructor
//onlyInstance = President::GetInstance(); cannot access operator'='

cout << "The name of the President is: ";
cout << President::GetInstance().GetName() << endl;

return 0;
}

OutPut:

​ The name of the President is: JackHvikee

  • Class that not allowed to be instanced in Stack

    The space of stack is limited. If you wanna write a database class, and it contains serveral TB data, maybe you should prohibit instancing it in stack, but instant it in free storage area. So, the key step is make sure destructor is Privative.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class MonsterDB
    {
    private:
    ~MonsterDB(); //private destructor
    };
    //and you can't create instance like this:
    int main()
    {
    MonsterDB myDatabase;//create instance in stack;

    return 0;
    }

    Above codes try to create instance in stack. When back stack there will popup all objects in stack, So compiler need invoke destructor ‘~MonsterDB’ in the end of ‘main()’, but the destructor is privative, i.e. not available, so above statements will result in compile error.

  • Declaring a destructor private does not prohibit the instantiation in the heap:

    1
    2
    3
    4
    5
    6
    int main()
    {
    MonsterDB* myDatabase = new MonsterDB();//no error cause in the heap.

    return 0;
    }

    Above codes will result in memory leak. You can’t invoke destructor in the main() func, so you can’t invoke ‘delete’.

    You need provide a static public func which could destroy instance (As class member, it can invoke destructor).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    class MonsterDB
    {
    private:
    ~MonsterDB()
    {
    };
    public:
    static void DestoryInstance(MonterDB* pInstance)
    {
    delete pInstance; //member can invoke private destructor
    }
    void DoSomething(){};
    };

    int main()
    {
    MonsterDB* myDB = new MonsterDB();//on heap
    myDB->DoSomething();

    //delete myDB; //Private destructor cannot be invoked

    //use static member to release memory
    MonsterDB::DestoryInstance(myDB);

    return 0;
    }
Share