Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unexpected behaviour

Reply
Thread Tools

Unexpected behaviour

 
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-23-2004
For the code


#include <iostream>

class Blah
{
int i;

public:

Blah(const Blah &obj)
{
std::cout<<"Copy Constructor called!\n";
i=obj.i;
}

Blah()
{
std::cout<<"Default Constructor called!\n"; i=0;
}
};


int main()
{
Blah poo1(Blah());

Blah poo2=Blah();
}


I only get

C:\c>temp
Default Constructor called!

C:\c>


which is produced for the second object while nothing is produced for
the first.


Why no message is produced for the first, and why only a default
constructor message is produced for the second?



I expected:

C:\c>temp
Default Constructor called!
Copy Constructor called!
Default Constructor called!
Copy Constructor called!

C:\c>



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-23-2004

"Ioannis Vranos" <(E-Mail Removed)> wrote in message
news:ciuoco$1q6s$(E-Mail Removed)...
> For the code
>
>
> #include <iostream>
>
> class Blah
> {
> int i;
>
> public:
>
> Blah(const Blah &obj)
> {
> std::cout<<"Copy Constructor called!\n";
> i=obj.i;
> }
>
> Blah()
> {
> std::cout<<"Default Constructor called!\n"; i=0;
> }
> };
>
>
> int main()
> {
> Blah poo1(Blah());
>
> Blah poo2=Blah();
> }
>
>
> I only get
>
> C:\c>temp
> Default Constructor called!
>
> C:\c>
>
>
> which is produced for the second object while nothing is produced for
> the first.
>
>
> Why no message is produced for the first, and why only a default
> constructor message is produced for the second?
>


Because this

Blah poo1(Blah());

is a function prototype. You can check this by adding the function call at
the end of main.

poo1(0);

Have you been listening to JKop again?

john


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-23-2004
Ioannis Vranos wrote:
> For the code
>
>
> #include <iostream>
>
> class Blah
> {
> int i;
>
> public:
>
> Blah(const Blah &obj)
> {
> std::cout<<"Copy Constructor called!\n";
> i=obj.i;
> }
>
> Blah()
> {
> std::cout<<"Default Constructor called!\n"; i=0;
> }
> };
>
>
> int main()
> {
> Blah poo1(Blah());
>
> Blah poo2=Blah();
> }
>
>
> I only get
>
> C:\c>temp
> Default Constructor called!
>
> C:\c>
>
>
> which is produced for the second object while nothing is produced for
> the first.


The statement

Blah poo1(Blah());

is a _declaration_ of a function 'poo1'. Read the FAQ.

>
>
> Why no message is produced for the first, and why only a default
> constructor message is produced for the second?


Because 'poo1' is not an object.

>
>
>
> I expected:
>
> C:\c>temp
> Default Constructor called!
> Copy Constructor called!
> Default Constructor called!
> Copy Constructor called!


Too bad. Study the declaration syntax.

>
> C:\c>
>
>
>


V
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-23-2004
> Because this
>
> Blah poo1(Blah());
>
> is a function prototype. You can check this by adding the

function call
> at the end of main.
>
> poo1(0);
>
> Have you been listening to JKop again?
>
> john



Now that's embarassing...


-JKop
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-23-2004
> >
> > Have you been listening to JKop again?
> >
> > john

>
>
> Now that's embarassing...
>
>
> -JKop


There was something about Ioannis' code style that gave the game away.

john


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-23-2004
John Harrison wrote:

> Have you been listening to JKop again?


Ehehehe, we have an expression here in Greece. When you are talking with
someone telling wrong things, confusion comes out and you say "I will
forget even what I know".

That is what happened here, I got confused.


That said, his intention was good.


BTW in the expression Blah poo=Blah() why not the copy constructor nor
the default constructor are called?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-23-2004
Fixed:


BTW in the expression Blah poo=Blah() why only the default constructor
is called?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-23-2004
Ioannis Vranos wrote:
> Fixed:
>
>
> BTW in the expression Blah poo=Blah() why only the default constructor
> is called?


Because it is allowed to be optimized to do so.

V
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-23-2004
Victor Bazarov wrote:

> Because it is allowed to be optimized to do so.




However consider this:


#include <iostream>

struct Blah
{
public:

int i;

Blah(const Blah &obj)
{
std::cout<<"Copy Constructor called!\n";

i=10*obj.i;
}

Blah()
{
std::cout<<"Default Constructor called!\n";

i=1;
}

Blah &operator=(const Blah &obj)
{
std::cout<<"Assignment used!\n";

i=5*obj.i;

return *this;
}

};


int main()
{
Blah poo=Blah();

std::cout<<poo.i<<std::endl;
}



C:\c>temp
Default Constructor called!
1

C:\c>



Any explanation?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-23-2004
So in summary we have got:


T x = T():

For POD types it is equivalent to initialisation to 0 for built in types
and all members to 0 for structs.

[Addition of 2003]: For non-POD types without a default constructor
definition, also all members to 0.


-----------------------------------------------------------------------


For non-POD types with a default constructor definition, it is
equivalent to T x; - no temporary is created and copy constructor is not
called.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unexpected behaviour for dispatching events... Mark Java 4 01-12-2011 04:21 PM
Unexpected mod-python behaviour. Simon Wittber Python 1 02-06-2004 06:45 PM
ostringstream unexpected behaviour Old Wolf C++ 1 02-04-2004 04:38 AM
Unexpected python behaviour Richard Philips Python 2 11-28-2003 03:14 PM
unexpected map behaviour Steven Van den Berghe C++ 2 08-28-2003 11:59 AM



Advertisments