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);
friend String operator + (const char *Left,const String &Right);
//It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
private:
char *m_Buffer;
}
It would be nice if I could avoid this
friend String operator + (const String &Left,const String &Right);
since String can be easily substituded with a char *
a line like this
String Str1("Test");
String Str2("Test");
String Res = Str1 + Str2;
should be able to call this version, if the compiler could automatically
cast the string object into a char *
friend String operator + (const String &Left,const char *Right);
is there a way I can define a cast operator for this class, like
class String
{
public
operator const char *() const { return m_Buffer; }
private:
char *m_Buffer;
}
so that I can do things like the + above, it would work?
I know I have seen this done somewhere, but I can figure out how they did
it.
What I get is an error on the cout line saying
Thanks Ali 5 4995
How about writing a constructor that takes a char[] ?
Then the char[] would be converted into a String, and all you need would be
an operator + for 2 Strings. :)
....i hope.
/Carl
"Ali R." <no****@nospam. com> wrote in message
news:11******** ************@ne wssvr11.news.pr odigy.com... 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); friend String operator + (const char *Left,const String &Right);
//It would be nice if I could avoid this friend String operator + (const String &Left,const String &Right);
private: char *m_Buffer; }
It would be nice if I could avoid this friend String operator + (const String &Left,const String &Right); since String can be easily substituded with a char *
a line like this
String Str1("Test"); String Str2("Test");
String Res = Str1 + Str2;
should be able to call this version, if the compiler could automatically cast the string object into a char * friend String operator + (const String &Left,const char *Right);
is there a way I can define a cast operator for this class, like class String { public operator const char *() const { return m_Buffer; } private: char *m_Buffer; }
so that I can do things like the + above, it would work?
I know I have seen this done somewhere, but I can figure out how they did it. What I get is an error on the cout line saying
Thanks Ali
"Ali R." <no****@nospam. com> wrote in message news:11******** ************@ne wssvr11.news.pr odigy.com... Hi guys,
I have a string class, which is a wrapper for a char * with a few operators, like +
Sounds like what you want is a converting constructor, like the std::string
class has.
class String {
public:
String(const char*);
String();
....
};
Then you pretty much give a const char* to anything expecting a String and it will
be converted.
The operator char*() you defined does the opposite (and is probably ill-advised).
It converts you String class to a const char*.
"Ali R." <no****@nospam. com> wrote in message
news:11******** ************@ne wssvr11.news.pr odigy.com... 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);
This is the one you can get rid of easily, by providing a non-explicit
String constructor taking a const char*. That's how the standard
library does it:
String(const char*);
<snip> is there a way I can define a cast operator for this class, like class String { public operator const char *() const { return m_Buffer; } private: char *m_Buffer; }
This should work. There are some well-known problems that can arise
with conversion operators, which you can find discussed in books like
Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this
group.) This is why the standard library uses converting constructors
but explicit member functions (c_str() and data()) for converting to
const char*.
What I get is an error on the cout line saying
Thanks Ali
I wish my compiler knew my name, and was so polite!
Jonathan
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:c0******** *****@ID-216073.news.uni-berlin.de... "Ali R." <no****@nospam. com> wrote in message news:11******** ************@ne wssvr11.news.pr odigy.com... 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); This is the one you can get rid of easily, by providing a non-explicit String constructor taking a const char*. That's how the standard library does it:
String(const char*);
<snip> is there a way I can define a cast operator for this class, like class String { public operator const char *() const { return m_Buffer; } private: char *m_Buffer; }
This should work. There are some well-known problems that can arise with conversion operators, which you can find discussed in books like Effective C++, Modern C++ Design, ... (and maybe also the FAQ for this group.) This is why the standard library uses converting constructors but explicit member functions (c_str() and data()) for converting to const char*.
What I get is an error on the cout line saying
Thanks Ali
LOL, I fogot to cut and paste the error line! And I can't type today
I wish my compiler knew my name, and was so polite!
Jonathan
Thanks Guys,
Ali
"Ali R." <no****@nospam. com> wrote in message
news:11******** ************@ne wssvr11.news.pr odigy.com... 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); friend String operator + (const char *Left,const String &Right);
//It would be nice if I could avoid this friend String operator + (const String &Left,const String &Right);
private: char *m_Buffer; }
It would be nice if I could avoid this friend String operator + (const String &Left,const String &Right); since String can be easily substituded with a char *
a line like this
String Str1("Test"); String Str2("Test");
String Res = Str1 + Str2;
should be able to call this version, if the compiler could automatically cast the string object into a char * friend String operator + (const String &Left,const char *Right);
is there a way I can define a cast operator for this class, like class String { public operator const char *() const { return m_Buffer; } private: char *m_Buffer; }
so that I can do things like the + above, it would work?
I know I have seen this done somewhere, but I can figure out how they did it. What I get is an error on the cout line saying
Thanks Ali
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: martin |
last post by:
hi guys,
have you ever tried to create a type-cast operator to a
'reference/pointer to an array'? i hadn't, not until yesterday when i
discovered that it is not exactly the trivial task one'd assume it to
be - none of my apparently naive attempts were accepted for a valid
type-cast operator by any of the compilers i threw them at (i.e. gcc
2.95.3, gcc 3.3.3, vc 2003).
so assume we have:
|
by: Arno |
last post by:
Hello,
I want to write classes, which can be used as interchangeably as
possible. The background is that they are smartpointers, which should
implicitly cast between each other.
As a toy example, let's take two classes A and B:
struct A {
A() {}
|
by: pnsteiner |
last post by:
i want to use the << operator defined for ostream with an object that
itself knows nothing of the operator, but is castable to ostream&. it
seems that C++ doesn't do implicit casts before invoking a custom
operator.
i've tested with visual c++ 7 and the comeau online compilers, both
required an explicit cast. is there a way to hint the compiler at using
the cast operator implicitely, or is this not part of the language at
all? (i have a...
| |
by: GlennDoten |
last post by:
I just happened to be looking through the implementation of the
System.Version class in the SSCLI and one of the constructors starts
like this:
public Version(String version) {
if ((Object) version == null)
throw new ArgumentNullException("version");
Does anyone have any ideas why the string parameter is being cast to an
object prior to checking for null? Is there some circumstance where not
|
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: Frederick Gotham |
last post by:
Before I begin, here's a list of assumptions for this particular example:
(1) unsigned int has no padding bits, and therefore no invalid bit-
patterns or trap representations.
(2) All types have the same alignment requirements.
(3) sizeof(double) >= sizeof(unsigned)
=====================================
We can cast from double to unsigned int as follows:
|
by: JoseMariaSola |
last post by:
How may operators and operands does (typename) expression has?
I'd say one operator, the cast operator, and two operands: typename
and expression.
But everywhere I read, cast is categorized as an unary operator. Why
is that? Is it just a syntax cotegory?
Thanks.
|
by: * Tong * |
last post by:
Hi,
I couldn't figure out how to properly type cast in this case:
$ cat -n type_cast.c
1 #include <stdio.h>
2
3 typedef unsigned char Byte;
4 typedef signed char Small_Int;
5
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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: 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...
| |