Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Conversion Operator question (http://www.velocityreviews.com/forums/t289404-conversion-operator-question.html)

Bill97 03-11-2005 02:14 AM

Conversion Operator question
 

In my code below, the conversion operator is invoked implicitly (and
explicitly) in main(). In the first case, the address value is output--in
the later cases the character string pointed to by the address is output.

I am looking for a satisfying answer as to why the character string isn't
output in the first of the 3 cases? Thank you for your reply


class String
{
public:
String(char * s)
{
strcpy(A, s);
}

operator char *()
{
cout << "conv. op. called" << endl;
return A;
}

private:
char A[100];
};


int main()
{

String pet("cat");

cout << pet << endl;

cout << (( char *)pet) << endl;

cout << static_cast< char *>(pet) << endl;


return 0;
}



Raghu Uppalli 03-11-2005 05:17 AM

Re: Conversion Operator question
 
It is printed. Here is what I get when I run this..

conv. op. called
cat
conv. op. called
cat
conv. op. called
cat

g++ 3.4.2


Bill97 03-11-2005 05:30 AM

Re: Conversion Operator question
 

"Raghu Uppalli" <raaghu@rocketmail.com> wrote in message
news:1110518249.889583.284690@o13g2000cwo.googlegr oups.com...
> It is printed. Here is what I get when I run this..
>
> conv. op. called
> cat
> conv. op. called
> cat
> conv. op. called
> cat
>
> g++ 3.4.2
>

Thanks for letting me know that. That was what I wanted to get. Using MS
Visual Studio 6.0, I got an address, followed by two cats. Conversion
operator was called all 3 times.

The only explanation I can think of is that perhaps a char* value is
converted to some type of string object (and only one conversion can be
applied).




All times are GMT. The time now is 10:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.