0

I have the following test in g++ 4.8.1 :

g++ -std=c++11 testclass.cpp -o testclass.exe

template<typename T>
class XRef
{
    private :
        int inum ;
        T * ptr ;
        bool owner ;
    public :
        XRef(int i,T *ptrx):inum{i},ptr{ptrx},owner{true}
        {cout << "natural" << endl ;}

        XRef(XRef& x):inum{x.inum},ptr{x.ptr},owner{false}
        {cout << "copy" << endl ;}

        XRef& operator=(XRef& x)
        {
            inum = x.inum ;
            ptr = x.ptr ;
            owner = false ;
            cout << "assign" << endl ;
            return *this ;
        }

        XRef(XRef&& x):inum{x.inum},ptr{move(x.ptr)},owner{true}
        {cout << "move" << endl ;}

        ~XRef()
        {
            if(owner)
                delete ptr ;
        }
} ;

int main()
{
    char *ptr1 ;
    char *ptr2 ;
    ptr1 = (char *) malloc(100) ;
    ptr2 = (char *) malloc(100) ;

    XRef<char> x1 = XRef<char>(1,ptr1) ;
    cout <<"==============" << endl ;
    XRef<char> x2 = x1 ;
    cout <<"==============" << endl ;
    XRef<char> x3(x2) ;
    cout <<"==============" << endl ;
    XRef<char> x4(XRef<char>(123,ptr2)) ;
    cout <<"==============" << endl ;
    XRef<char> x5(move(XRef<char>(123,ptr2))) ;
    cout <<"==============" << endl ;
    XRef<char> x6{123,ptr2} ;
}

Then , the output :

natural
==============
copy
==============
copy
==============
natural
==============
natural
move
==============
natural

What surprise me is that : XRef x2 = x1 ; , I think this should call XRef& operator=(XRef& x) , but this test showes it call XRef(XRef& x) instead ....

I like to know what i do is wrong , so that operator= is not called !!

Edit :

XRef<char> x7{123,ptr2} ;
cout <<"==============" << endl ;
x7 = x6 ;
cout <<"==============" << endl ;

Showes :

natural
==============
assign
==============

So ,

XRef<char> x2 = x1 ;  

is different with

XRef<char> x7{123,ptr2} ;
x7 = x6 ;

How come this happened ?

PS. I refered to : copy construtor called extra for reference ...

Community
  • 1
  • 1
barfatchen
  • 1,630
  • 2
  • 24
  • 48
  • The line `XRef x2 = x1 ;` is __creating a new object__ so the copy constructor is called (even though it looks something like an assignment). [See this related answer.](http://stackoverflow.com/a/5368273/445976) – Blastfurnace Jul 05 '13 at 02:24
  • Thank you,Blastfurnace,that is a excellent information to me ~!! – barfatchen Jul 05 '13 at 02:27
  • Your code has Undefined Behavior since it invokes double freeing memory. – awesoon Jul 05 '13 at 02:43
  • Thanks you,soon,this is still a test case , some works under debug, it is very kind of you for the kind information !! – barfatchen Jul 05 '13 at 03:06
  • You should either do the memory allocation-deallocation with `new[]` and `delete[]` OR with `malloc` and `free`. What you currently have may accidentally work but it is undefined behavior and tools like valgrind would catch these type of bugs. – Ali Jul 05 '13 at 09:09
  • Thanks, Ali , I got your point , appreciate your suggestion !! – barfatchen Jul 08 '13 at 00:17

0 Answers0