Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:25 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: operator int()
PostPosted: Fri Nov 07, 2008 6:08 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:47 pm
Posts: 281
Has thanked: 0 time
Have thanks: 1 time

I got a question. I have a class B that has this method:
Code:
operator int()
{
std::cout << "int()" << std::endl;
return i;
}


I dont know exactly how it works but every time I call this method:

Code:
int foo1(B b)
{
return b;
}


it prints int().


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: operator int()
PostPosted: Fri Nov 07, 2008 6:09 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:53 pm
Posts: 229
Has thanked: 0 time
Have thanks: 0 time
Implicit class conversion to another type using operator overloading.
The C++ compiler makes a best-guess effort at creating a successful
compile by looking at the existing class methods and the type the code
is expecting and then creating the necessary code as output. This is,
IMO, one of the coolest features of C++ (you can even "typecast" between
classes!) but, occasionally, one of the more annoying ones should you
depend on the feature too much. That is, you can "see" a clear path to
the implicit solution but the compiler insists there isn't one.


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 operator overloading
 working of bitwise ~ operator
 stream operator
 unary operator overloading
 Sizeof Operator
 What is the % operator
 difference between the Boolean & operator and the &&
 Using the ? Operator
 trinary operator
 Operator Precedence table

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team