C ++ - smart pointer, logical operators trap

A smart pointer

Memory leak (C ++ main source Bug)
1. Dynamic application heap space, not returned after use
2.C ++ language no garbage collection
3. The stack pointer can not control the life cycle of the spatial
code sample

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int i;
public:
    Test(int i)
    {
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
    }
};

int main()
{
    for(int i=0; i<5; i++)
    {
        Test* p = new Test(i);

        cout << p->value() << endl;
    }

    return 0;
}

The results shown in FIG operation
C ++ - smart pointer, logical operators trap
when the output result can be seen as expected, but the pointer is not to be released after the application memory, but a large amount of data is present, the program will crash
demand smart pointer with the object
1. require a special pointer
2. pointer actively releasing heap space at the end of the life cycle
3. a heap must have at most a pointer identifying
4. put pointer arithmetic comparison where the pointer
solution
1. overload characteristic operator pointer (->)
2. only by overloaded member function
3. the parameter can not be overloaded function
4. overloaded function can define a
code sample

#include <iostream>
#include <string>
//智能指针的实现
using namespace std;

class Test
{
    int i;
public:
    Test(int i)
    {
        cout << "Test(int i)" << endl;
        this->i = i;
    }
    int value()
    {
        return i;
    }
    ~Test()
    {
        cout << "~Test()" << endl;
    }
};

class Pointer
{
    Test* mp;
public:
    Pointer(Test* p = NULL)
    {
        mp = p;
    }
    Pointer(const Pointer& obj)
    {
        mp = obj.mp;
        const_cast<Pointer&>(obj).mp = NULL;
    }
    Pointer& operator = (const Pointer& obj)
    {
        if( this != &obj )
        {
            delete mp;
            mp = obj.mp;
            const_cast<Pointer&>(obj).mp = NULL;
        }

        return *this;
    }
    Test* operator -> ()
    {
        return mp;
    }
    Test& operator * ()
    {
        return *mp;
    }
    bool isNull()
    {
        return (mp == NULL);
    }
    ~Pointer()
    {
        delete mp;
    }
};

int main()
{
    Pointer p1 = new Test(0);

    cout << p1->value() << endl;

    Pointer p2 = p1;

    cout << p1.isNull() << endl;

    cout << p2->value() << endl;

    return 0;
}

As shown in FIG operating results
C ++ - smart pointer, logical operators trap
can be seen after the pointer for heap space used up, it will automatically release the memory
summary
1 wherein the pointer can be overloaded operator
2. Overload signature pointer can be used in place of the object pointer
3. smart pointers can only be used to point to heap memory space of
meaning 4. smart pointers is to avoid the greatest degree of memory problems

Two logical operators trap

A. Native semantic logical operator
1 only two operand value (true and to false)
2. logical expression can be determined without fully calculated final value
3. The final result can only be false true or
code sample

#include <iostream>
#include <string>

using namespace std;

int func(int i)
{
    cout << "int func(int i) : i = " << i << endl;

    return i;
}

int main()
{
    if( func(0) && func(1) )
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }

    cout << endl;

    if( func(0) || func(1) )
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }

    return 0;
}

C ++ - smart pointer, logical operators trap
Q: logical operators can override it? Overloaded logical operators What is the significance?
Code Example and Results

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int mValue;
public:
    Test(int v)
    {
        mValue = v;
    }
    int value() const
    {
        return mValue;
    }
};

bool operator && (const Test& l, const Test& r)
{
    return l.value() && r.value();
}

bool operator || (const Test& l, const Test& r)
{
    return l.value() || r.value();
}

Test func(Test i)
{
    cout << "Test func(Test i) : i.value() = " << i.value() << endl;

    return i;
}

int main()
{
    Test t0(0);
    Test t1(1);

    if( func(t0) && func(t1) )
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }

    cout << endl;

    if( func(1) || func(0) )
    {
        cout << "Result is true!" << endl;
    }
    else
    {
        cout << "Result is false!" << endl;
    }

    return 0;
}

C ++ - smart pointer, logical operators trap
And the code can be seen from the results of operation, regardless of the logical operators whether to replace the sequence ends, both ends of the data calculation will
B. nature of the problem analysis
1.C ++ function calls extended by the function operator
2. The function must be entered before calculation is done for all parameters
calculating order 3. the function parameters is uncertain
4. the complete failure of short-circuit rules
recommended: the actual project development to avoid overloading logical operators, comparison operators overloaded by substituting the logical operator overloading, instead of directly using the member functions logical operator overloading, global function using logical operator overloading
Summary
1.C ++ supports logical operators syntactically overloaded
2. after the short circuit does not satisfy the logical operator overloading rule
3. Do not overload engineering logical operators
4 by weight comparison operators carrying replacement logic operator overloading
The replacement logic through dedicated operator overloading member functions

Guess you like

Origin blog.51cto.com/13475106/2412496