Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conversion Operator question

Reply
Thread Tools

Conversion Operator question

 
 
Bill97
Guest
Posts: n/a
 
      03-11-2005

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;
}


 
Reply With Quote
 
 
 
 
Raghu Uppalli
Guest
Posts: n/a
 
      03-11-2005
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

 
Reply With Quote
 
 
 
 
Bill97
Guest
Posts: n/a
 
      03-11-2005

"Raghu Uppalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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).


 
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
operator << and conversion operator cronusf C++ 12 10-28-2010 09:21 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
<complex> : no match for 'operator*' // conversion operator double() Arvid Requate C++ 2 06-23-2006 10:41 AM
conversion operator question hunter hou C++ 1 02-10-2006 06:48 AM



Advertisments