473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Constructors in C++

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/

Mar 8 '06 #1
4 1711
WahJava <wa*****@gmail.com> wrote:
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
--
jb

(reply address in rot13, unscramble first)
Mar 8 '06 #2
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
--
http://wahjava.blogspot.com/

Mar 8 '06 #3
WahJava wrote:
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.

Mar 8 '06 #4
Hi Rolf,

Rolf Magnus wrote:
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
--
http://wahjava.blogspot.com/

Mar 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.