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 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.
"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
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
"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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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:
/* ... */
|
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...
|
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?
|
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
| |
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.
|
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
|
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 ;
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |