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 4978
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********************@newssvr11.news.prodigy .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********************@newssvr11.news.prodigy .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********************@newssvr11.news.prodigy .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******@kangaroologic.com> wrote in message
news:c0*************@ID-216073.news.uni-berlin.de... "Ali R." <no****@nospam.com> wrote in message news:11********************@newssvr11.news.prodigy .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********************@newssvr11.news.prodigy .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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |