cast operator for built in to class?

G

Gernot Frisch

Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::eek:perator string() const;

can I define such a cast operator for const char* instead of class C??


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
J

Jacques Labuschagne

Gernot said:
Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::eek:perator string() const;

can I define such a cast operator for const char* instead of class C??

Surely you can just define a (non-explicit) constructor for C that takes
a const char* as its argument... Then something like
C c;
c = "hello world";
would create a nameless temporary using C::C(const char*) and copy to
'c' using C::eek:perator=(const C&).

Jacques.
 
G

Gernot Frisch

Jacques Labuschagne said:
C??

^^^^^^^^^^^^^^^^^^

Surely you can just define a (non-explicit) constructor for C that takes
a const char* as its argument... Then something like
C c;
c = "hello world";
would create a nameless temporary using C::C(const char*) and copy to
'c' using C::eek:perator=(const C&).

That would mean I have to define class "C", but I just want to have
class "string"

-Gernot
 
K

Karl Heinz Buchegger

Gernot said:
Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::eek:perator string() const;

can I define such a cast operator for const char* instead of class C??

If I understand you, you want something like this

operator const char*( string txt )
{
return txt.c_str();
}

The answer is: No. Conversion operators have to be a member of a class.
 
G

Gernot Frisch

Karl Heinz Buchegger said:
C??

If I understand you, you want something like this

operator const char*( string txt )
{
return txt.c_str();
}

The answer is: No. Conversion operators have to be a member of a
class.

Since the above provides an obvoisly unambigious operator of which the
usage is totally clear to everybody - why doesn't standart C++ allow
it?

Anyway - thank you, I can deal with it with a wrapper class around it.
 
K

Karl Heinz Buchegger

Gernot said:
class.

Since the above provides an obvoisly unambigious operator of which the
usage is totally clear to everybody - why doesn't standart C++ allow
it?

Don't know.
You have to ask the guys down at comp.std.c++ why this is so.
There all the guys defining the standard hang around and discuss
what changes should be done to the language.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top