Constructors in C++

W

WahJava

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 !!
 
J

Jakob Bieling

WahJava said:
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 << ']';
}

Constructors are no functions in that sense. You cannot call them,
like you can call a function. The constructor is called for you. In the
above code (which I rearranged for better readability), you /create a
temporary/ object with the line "Obj::Obj(0);" which goes out of scope
as soon as the ';' is reached.

If you want constructors to share common code, have all of them call
a private member function, which does the all the common member
assignments.

Hope this explains why 'value' of the 'o' object contains random
junk.

regards
 
W

WahJava

Hi jb,

Thanx a ton. How idiot I'm.

BTW, what is

Obj p = 30;

Is it a copy constructor invocation ( I don't think so ) ?

Thanx,
Ashish Shukla
 
R

Rolf Magnus

WahJava said:
Hi jb,

Thanx a ton. How idiot I'm.

BTW, what is

Obj p = 30;

Is it a copy constructor invocation ( I don't think so ) ?

It is, as well as a conversion constructor invocation. What happens is that
30 is converted into an Obj, using your Obj::Obj(int x) constructor. That
Obj is then copied to p using the copy constructor.
Just in case you wonder that your copy constructor is actually not called:
The compiler is allowed to optimize the copy away and directly use the
conversion constructor to create p.
 
W

WahJava

Hi Rolf,

Rolf said:
It is, as well as a conversion constructor invocation. What happens is that
30 is converted into an Obj, using your Obj::Obj(int x) constructor. That
Obj is then copied to p using the copy constructor.
Just in case you wonder that your copy constructor is actually not called:
The compiler is allowed to optimize the copy away and directly use the
conversion constructor to create p.

Actually I was updating my Classic C++ knowledge to Modern C++. And I
think this is in modern C++ not in classic. And in order to avoid such
conversion constructor calls I've to mark my that constructor
"explicit" . Am I right naa... ?

Thanx for replying,
Ashish Shukla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top