Monday 28 July 2014

[C++] Constructor/Copy Constructor/Assignment Operator/Destructor

1) Both Copy Constructor and Assignment Operator are call by address, not call by value
#include <iostream>

using namespace std;

class Array {
    public:
        int *_array;
        int size;

        Array(int input):size(input)
        {
            _array = new int[size];
        }

        void Set(int i, int v)
        {
            _array[i] = v;
        }

        int Get(int i)
        {
            return _array[i];
        }

        ~Array()
        {
            delete [] _array;
        }
};

int main()
{
    Array a(5);
    Array b = a;

    a.Set(0, 10);
    a.Set(1, 20);
    a.Set(2, 30);
    a.Set(3, 40);
    a.Set(4, 50);

    cout << a.Get(0) << " " << a.Get(1) << " " << a.Get(2) << " " << a.Get(3) << " " << a.Get(4) << endl;
    cout << b.Get(0) << " " << b.Get(1) << " " << b.Get(2) << " " << b.Get(3) << " " << b.Get(4) << endl;
    cout << &a << " " << a._array << endl;
    cout << &b << " " << b._array << endl;

    return 0;
}
10 20 30 40 50
10 20 30 40 50
0x7fff61f44a90 0x1180010
0x7fff61f44aa0 0x1180010
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000001180010 ***

the problem caused in this program:
- memory leak in b
- it was copied by "b._array = a._array;", either modified in b or a will change the result
- double free

2) Initialization list
#include <iostream>

using namespace std;

class A {
    public:
        A()
        {
            cout << "A constructor" << endl;
        }

        A(const A& a)
        {
            cout << "A copy constructor" << endl;
        }

        A& operator=(const A& a)
        {
            cout << "A assignment Operator" << endl;
        }
};

class B {
    public:
        B(A &input)
        {
            cout << "B consturctor" << endl;
            m_a = input;
        }

    private:
        A m_a;
};

class C {
    public:
        C(A& input):m_a(input)
        {
            cout << "C constructor" << endl;
        }

    private:
        A m_a;
};

int main()
{
    cout << "===A===" << endl;
    A a;
    cout << "===B===" << endl;
    B b(a);
    cout << "===C===" << endl;
    C c(a);

    return 0;
}
===A===
A constructor
===B===
A constructor
B consturctor
A assignment Operator
===C===
A copy constructor
C constructor

3) Const variable can only be initialed in initialization list
#include <iostream>

using namespace std;

class A {
    public:
        A(int input) { a = input; }
        void show() { cout << a << endl; }

    private:
        const int a;
};

int main()
{
    A aa(5);
    aa.show();

    return 0;
}
test2.cpp: In constructor ‘A::A(int)’:
test2.cpp:7:9: error: uninitialized member ‘A::a’ with ‘const’ type ‘const int’ [-fpermissive]
test2.cpp:7:28: error: assignment of read-only member ‘A::a’
#include <iostream>

using namespace std;

class A {
    public:
        A(int input):a(input) { }
        void show() { cout << a << endl; }

    private:
        const int a;
};

int main()
{
    A aa(5);
    aa.show();

    return 0;
}
5

4) The serial of constructor and destructor
#include <iostream>

using namespace std;

class A {
    public:
        A()
        {
            cout << "A constructor" << endl;
        }

        ~A()
        {
            cout << "A destructor" << endl;
        }
};

class B {
    public:
        B()
        {
            cout << "B constructor" << endl;
        }

        ~B()
        {
            cout << "B destructor" << endl;
        }
};

class C {
    public:
        C()
        {
            cout << "C constructor" << endl;
        }

        ~C()
        {
            cout << "C destructor" << endl;
        }
};

class Aa : public A {
    public:
        Aa(int in1):y(in1)
        {
            cout << "Aa constructor" << endl;
        }

        ~Aa()
        {
            cout << "Aa destructor" << endl;
        }

    private:
        B b;
        int y;
        C c;
};

int main()
{
    Aa hi(5);

    return 0;
}
A constructor
B constructor
C constructor
Aa constructor
Aa destructor
C destructor
B destructor
A destructor

No comments:

Post a Comment