cout printing and function tracing

J

Jacopo

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

James Kanze

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

Jacopo

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)
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 ?
 
I

Ian Collins

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

Jacopo

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 ?
 
P

Paul

Jacopo said:
Yeah sorry, it was the copy/paste fault.


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

Jacopo

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. :)
 
I

Ian Collins

Yeah sorry, it was the copy/paste fault.


....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.
 
P

Paul

Jacopo said:
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.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top