[Bug c++/37911] New: Operator= called on a non constructed object.

2008-10-24 Thread wonsjb at gmail dot com
If I compile this code:
(g++ -o element element.cpp)

#include 
#include 

using namespace std;


class element
{
public:
element()
{
cout << "element() = " << this << endl;
}
element(const element &e)
{
cout << "element(const element &e="<< &e << ") = " << this << endl;
}
~element()
{
cout << "~element() = " << this << endl;
}
element &operator=(const element &e)
{
cout << "operator=(const element &e="<< &e << ") = " << this << endl;
}
string getString() {return a;}
private:
string a;
};

element parse()
{
element aelement;
return aelement;
}

element parse2()
{
element aelement;
return aelement;
}

bool toto=true;

int main()
{
element el = toto ? el = parse() : parse2();
return 0;
}



It compiles without warning or error.

However, the result is:
element() = 0xfeffc680
operator=(const element &e=0xfeffc680) = 0xfeffc67c
element(const element &e=0x8049ea8) = 0xfeffc67c
~element() = 0xfeffc680
~element() = 0xfeffc67c


You can see that operator= is called on a object of address 0xfeffc67c before
this object is constructed.

Regards,
Jean-Baptiste


-- 
   Summary: Operator= called on a non constructed object.
   Product: gcc
   Version: 4.0.4
            Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: wonsjb at gmail dot com
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37911



[Bug c++/37911] Operator= called on a non constructed object.

2008-10-24 Thread wonsjb at gmail dot com


--- Comment #2 from wonsjb at gmail dot com  2008-10-24 17:58 ---
I am not sure what should be the right behavior here.
Maybe a compile error.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37911