Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout printing and function tracing

Reply
Thread Tools

cout printing and function tracing

 
 
Jacopo
Guest
Posts: n/a
 
      02-10-2011
Hi, I'm running this code:

class C {
private:
int d;
public:
C(string s=""): d(s.size()) {}
explicit C(int n): d(n) { cout<<"*1*"; } // 1
operator int() { cout<<"*2*";return d; } // 2
C operator+(C x) { cout<<"*3*";return C(d+x.d); } // 3
};

main() {
C a, b("pippo"), c(3); cout<<std::endl;
cout << a << ' ' << 1+b << ' ' << c+4 << ' ' << c+b;
}


It prints:
*1*
*3**1**2**2**2**2*0 6 7 8
Look at the second line: ok for the last four numbers, but output
produced from function calls seems to start from the end (c+b
evaluation), in reverse order.
I'm not sure why, so could someone clarify this behaviour ?

Thanks.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      02-10-2011
On Feb 10, 4:45 pm, Jacopo <(E-Mail Removed)> wrote:
> Hi, I'm running this code:


> class C {
> private:
> int d;
> public:
> C(string s=""): d(s.size()) {}
> explicit C(int n): d(n) { cout<<"*1*"; } // 1
> operator int() { cout<<"*2*";return d; } // 2
> C operator+(C x) { cout<<"*3*";return C(d+x.d); } // 3
> };


> main() {
> C a, b("pippo"), c(3); cout<<std::endl;
> cout << a << ' ' << 1+b << ' ' << c+4 << ' ' << c+b;
> }


> It prints:
> *1*
> *3**1**2**2**2**2*0 6 7 8
> Look at the second line: ok for the last four numbers, but output
> produced from function calls seems to start from the end (c+b
> evaluation), in reverse order.


The order function arguments are evaluated is unspecified. The
only order guaranteed in your output is that the characters
"0 6 7 8" appear in that order. The *n* can appear in
practically any order, and in almost any place within or before
these characters.

--
James Kanze
 
Reply With Quote
 
 
 
 
Jacopo
Guest
Posts: n/a
 
      02-10-2011
On 10 Feb, 19:51, James Kanze <(E-Mail Removed)> wrote:
>
> The order function arguments are evaluated is unspecified. *The
> only order guaranteed in your output is that the characters
> "0 6 7 8" appear in that order. *The *n* can appear in
> practically any order, and in almost any place within or before
> these characters.
>
> --
> James Kanze



Ok, thanks.
I've another question: nearly the same code, without "explicit"
keyword on C(int).

class C {
private:
int d;
public:
C(string s=""): d(s.size()) {}
C(int n): d(n)
operator int() {return d;}
C operator+(C x) {return C(d+x.d);}
};

main() {
C a, b("pippo"), c(3);
cout<<c+4;
}

It does not compile, giving an error of "ambiguous overload for
‘operator+’ in ‘c + 4’ ". I'm thinking that:
- the c+4 expression calls C operator+(C x)
- the "4" integer gets implicitly converted in a temporary C object
and then passed to x parameter
- and so the body executed
Where is the ambiguity ? Perhaps, with standard + operator ?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-10-2011
On 02/11/11 11:00 AM, Jacopo wrote:
> On 10 Feb, 19:51, James Kanze<(E-Mail Removed)> wrote:
>>
>> The order function arguments are evaluated is unspecified. The
>> only order guaranteed in your output is that the characters
>> "0 6 7 8" appear in that order. The *n* can appear in
>> practically any order, and in almost any place within or before
>> these characters.

>
> Ok, thanks.
> I've another question: nearly the same code, without "explicit"
> keyword on C(int).
>
> class C {
> private:
> int d;
> public:
> C(string s=""): d(s.size()) {}
> C(int n): d(n)


Missing {}?

> operator int() {return d;}
> C operator+(C x) {return C(d+x.d);}
> };
>
> main() {


missing int.

> C a, b("pippo"), c(3);
> cout<<c+4;
> }
>
> It does not compile, giving an error of "ambiguous overload for
> ‘operator+’ in ‘c + 4’ ". I'm thinking that:
> - the c+4 expression calls C operator+(C x)
> - the "4" integer gets implicitly converted in a temporary C object
> and then passed to x parameter
> - and so the body executed
> Where is the ambiguity ? Perhaps, with standard + operator ?


Yes, c can be converted to int, so you could have

c.operator int() + 4;

or

c + C(4)

Now you can see why conversion operators are a pain!

--
Ian Collins
 
Reply With Quote
 
Jacopo
Guest
Posts: n/a
 
      02-11-2011
On 10 Feb, 23:18, Ian Collins <(E-Mail Removed)> wrote:
>
> Missing {}?
>


Yeah sorry, it was the copy/paste fault.

> Yes, c can be converted to int, so you could have
>
> c.operator int() + 4;
>
> or
>
> c + C(4)
>
> Now you can see why conversion operators are a pain!
>


....I see.
From what I've understood, it's a matter of overload resolution. The
compiler creates a set of candidates function and selects the most
viable...but in the above case standard operator+ and redefined member
operator+ have the same "rank", so basically it cannot choose. Right ?

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-11-2011

"Jacopo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 10 Feb, 23:18, Ian Collins <(E-Mail Removed)> wrote:
>>
>> Missing {}?
>>

>
> Yeah sorry, it was the copy/paste fault.
>
>> Yes, c can be converted to int, so you could have
>>
>> c.operator int() + 4;
>>
>> or
>>
>> c + C(4)
>>
>> Now you can see why conversion operators are a pain!
>>

>
> ...I see.
> From what I've understood, it's a matter of overload resolution. The
> compiler creates a set of candidates function and selects the most
> viable...but in the above case standard operator+ and redefined member
> operator+ have the same "rank", so basically it cannot choose. Right ?
>
>

Hi I don't have a clue what this thread is about but I notiice Ian Collins
has commented on it. I cannot resist this opportunity to have a poke at Ian
so I say please be carefull about what Ian has to say. He appears to be a
typical example of an *average* programmer trying to pretend he is an
expert.
Thus my interest in this thread is only to analyze Ians competence.



 
Reply With Quote
 
Jacopo
Guest
Posts: n/a
 
      02-11-2011
On 11 Feb, 23:27, "Paul" <(E-Mail Removed)> wrote:
> Hi I don't have a clue what this thread is about but I notiice Ian Collins
> has commented on it. I cannot resist this opportunity to have a poke at Ian
> so I say please be carefull about what Ian has to say. He appears to be a
> typical example of an *average* programmer trying to pretend he is an
> expert.
> Thus my interest in this thread is only to analyze Ians competence.


Nice.
But if you want to check Ian's competence in this thread, I think you
should know the answer to my question.
So please, make me aware.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-12-2011
On 02/11/11 01:02 PM, Jacopo wrote:
> On 10 Feb, 23:18, Ian Collins<(E-Mail Removed)> wrote:
>>
>> Missing {}?
>>

>
> Yeah sorry, it was the copy/paste fault.
>
>> Yes, c can be converted to int, so you could have
>>
>> c.operator int() + 4;
>>
>> or
>>
>> c + C(4)
>>
>> Now you can see why conversion operators are a pain!
>>

>
> ....I see.
> From what I've understood, it's a matter of overload resolution. The
> compiler creates a set of candidates function and selects the most
> viable...but in the above case standard operator+ and redefined member
> operator+ have the same "rank", so basically it cannot choose. Right ?


The ambiguity comes from the conversion operator and constructor; c can
be converted to an int or 4 can be converted to a C.

If you make the constructor explicit, only the conversion operator can
be used and the ambiguity is removed.

--
Ian Collins
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      02-13-2011

"Jacopo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 11 Feb, 23:27, "Paul" <(E-Mail Removed)> wrote:
>> Hi I don't have a clue what this thread is about but I notiice Ian
>> Collins
>> has commented on it. I cannot resist this opportunity to have a poke at
>> Ian
>> so I say please be carefull about what Ian has to say. He appears to be a
>> typical example of an *average* programmer trying to pretend he is an
>> expert.
>> Thus my interest in this thread is only to analyze Ians competence.

>
> Nice.
> But if you want to check Ian's competence in this thread, I think you
> should know the answer to my question.
> So please, make me aware.
>

Don't want to get too tuned into C++ atm, I'm swtiching to Java.

GL.

 
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
Re: cout vs std::cout Hendrik Schober C++ 7 10-07-2008 12:02 AM
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments