473,770 Members | 4,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

cast operator for built in to class?

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%c gl%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
5 1379
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=(co nst C&).

Jacques.
Jul 22 '05 #2

"Jacques Labuschagne" <ja*****@clawsh rimp.com> schrieb im Newsbeitrag
news:Y5******** ***********@new s02.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=(co nst C&).


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

-Gernot
Jul 22 '05 #3
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

"Karl Heinz Buchegger" <kb******@gasca d.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
Gernot Frisch wrote:

"Karl Heinz Buchegger" <kb******@gasca d.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4995
by: Ali R. | last post by:
Hi guys, I have a string class, which is a wrapper for a char * with a few operators, like + What I am trying to do is reduce the number of overloaded operators so for insteance class String { friend String operator + (const String &Left,const char *Right);
2
7315
by: Ulrich Heinen | last post by:
Let's say I have two classes A and B with similar goals but completely different implementations. In my application code I want to use class A exclusively, but instances of B should also be accepted. This can be achieved when B provides an appropriate cast operator, e.g. class A { public: /* ... */ protected: /* ... */
0
1678
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large pieces of data around, and requires that certain methods return an Envelope with a specific kind of Letter as it's content. I have a cast operator that converts from what I've got to what should be returned, but it seems that the compiler only...
3
21221
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object obj=Activator.CreateInstance(strAssemblyName, "MyClass"); //Now, I want to do something like ((MyClass)obj).Method //Can I do this?
6
6729
by: Lore Leunoeg | last post by:
Hello I derived a class MyControl from the Control class. Public Class MyControl Inherits Control Sub New() MyBase.New() End Sub End Class
10
2202
by: Pieter Breed | last post by:
Hi All, Please excuse me, but the bulk of my post will be a code post. It describes some weirdness with regards to the implicit casting operator. The crux of the problem is this: I want to set a property on a class that takes an interface instance. I have a class that can cast implicit to a class that impliments said interface, but the compiler moans and says it cannot cast implicitly like that.
2
2764
by: John Goche | last post by:
Hello, Could anyone please provide with some information on the C++ overloaded cast operator and in which circumstances this might be useful? I have consulted several references but found no information on the uses of this operator. Thanks, JG
3
3282
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
8
2976
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.