Hi group,
I was reading "The C++ Programming Language, 3rd Edition" by "Bjarne
Stroustroup". And I wrote following program, while I was reading some
chapter (I forgot):
// constr.cc
#include <iostream>
using namespace std;
class Obj
{
int value;
public:
Obj() { clog << "invoked Obj() ["; Obj::Obj(0); clog << ']'; }
Obj(int x):value(x) { clog << "invoked Obj(" << value << ')'; }
int get() { return value; }
friend ostream& operator << (ostream&, Obj&);
};
ostream& operator << (ostream& out, Obj& x)
{
return out << "X: { " << x.value << " }";
}
int main()
{
Obj o;
Obj p = 300;
cout << "o: " << o << endl
<< "p: " << p << endl;
}
// program ends here
I used following command to compile with g++ [ (GCC) 4.0.0 20050519
(Red Hat 4.0.0-8) ] on AMD64 architecture:
$ g++ -o constr constr.cc
I'm expecting output to be:
/// output begins here ////
invoked Obj() [invoked Obj(0)]invoked Obj(300)o: X: { 0 }
p: X: { 300 }
/// output ends here ////
but it gave me:
/// output begins here ////
invoked Obj() [invoked Obj(0)]invoked Obj(300)o: X: { -754869280 }
p: X: { 300 }
/// output ends here ////
So is it a bug in my program. Or I'm expecting wrong thing. BTW, I'm
new to "Obj p = 30;" style of construction. Can anybody give
description of what it does ? I mean how it is able to construct Obj
instance. Probably giving references to "The C++ Programming Language,
3rd Edition" by "Bjarne Stroustroup"
Thanx in advance,
Ashish Shukla alias Wah Java !!
--
http://wahjava.blogspot.com/