By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

cast operator for built in to class?

P: n/a
Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::operator 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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Gernot Frisch wrote:
Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::operator 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::operator=(const C&).

Jacques.
Jul 22 '05 #2

P: n/a

"Jacques Labuschagne" <ja*****@clawshrimp.com> schrieb im Newsbeitrag
news:Y5*******************@news02.tsnz.net...
Gernot Frisch wrote:
Hi,

I can define a class C that can handle a const char* and provide an operator:
C::operator 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::operator=(const C&).


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

-Gernot
Jul 22 '05 #3

P: n/a
Gernot Frisch wrote:

Hi,

I can define a class C that can handle a const char* and provide an
operator:
C::operator 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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> schrieb im Newsbeitrag
news:41***************@gascad.at...
Gernot Frisch wrote:

Hi,

I can define a class C that can handle a const char* and provide an operator:
C::operator 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.

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.
Jul 22 '05 #5

P: n/a
Gernot Frisch wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> schrieb im Newsbeitrag
news:41***************@gascad.at...
Gernot Frisch wrote:

Hi,

I can define a class C that can handle a const char* and provide an operator:
C::operator 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.

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.