473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

friend operator= ?

There is a curious type of operator overloading with streams that takes
the form of:
friend ostream& operator<<(ostream &stream, String out);

Is it possible to define a similar type of friend operator, but rather
than using << use =?

What I would like to do is define conversion operations from high level
classes to low level ones. For example, I have my own String class
which is used everywhere. It is common that I am doing operations such
as:
someString = someInt; // so the string would get a value like
"123"
someString = somePoint; // the string might look like: "[123,
456]"

Up until now I have had to create operator specialization explicitly in
the String class for each type. This has the major drawback that the
String class must know about each type and I end up mingling core level
code with higher level code, which is no good.

In a perfect world I would like to have a class that defined its own
conversion to String implicitly. I know that I could certainly have a
method called ToString(), but I was hoping for something more
transparent. For example:

class SomeObject {
unsigned long ID;
String Name;

friend String& operator=(String &str, SomeObject &obj) {
str = obj.Name; return str; }
};

Of course this does not compile and I don't even know if such a feature
exists in C++.

Thanks

Dec 7 '05 #1
3 2970
On 7 Dec 2005 11:17:28 -0800, st***@walkereffects.com wrote:
There is a curious type of operator overloading with streams that takes
the form of:
friend ostream& operator<<(ostream &stream, String out);

Is it possible to define a similar type of friend operator, but rather
than using << use =?

What I would like to do is define conversion operations from high level
classes to low level ones. For example, I have my own String class
which is used everywhere. It is common that I am doing operations such
as:
someString = someInt; // so the string would get a value like
"123"
someString = somePoint; // the string might look like: "[123,
456]"
Isn't this doing low level to higher level conversion? Or does
"someInt" have some other type than int?
Up until now I have had to create operator specialization explicitly in
the String class for each type. This has the major drawback that the
String class must know about each type and I end up mingling core level
code with higher level code, which is no good.
Why re-invent the wheel? std::stringstream does all this perfectly
well.
In a perfect world I would like to have a class that defined its own
conversion to String implicitly. I know that I could certainly have a
method called ToString(), but I was hoping for something more
transparent. For example:

class SomeObject {
unsigned long ID;
String Name;

friend String& operator=(String &str, SomeObject &obj) {
str = obj.Name; return str; }
};

Of course this does not compile and I don't even know if such a feature
exists in C++.


It looks like you have attempted to define a member operator=() for
SomeObject in the above example. If you want to assign to String, you
need an assignment operator in String, not SomeObject. Besides, the
arguments are wrong for a member assignment operator.

There are big problems with unwanted implicit conversions which arise
when you define operators such as that. Better to use explicit
functions such as "ToString".

I would suggest using the STL stringstream class to implement all of
your conversions because that way, you don't need to re-invent the
wheel. Overload operator<< for ostream the usual way for your custom
classes, then use the str() member function to retrieve the string.
That has the advantage that you can also use the same stream functions
to read and write your data to a file, for example.

--
Bob Hairgrove
No**********@Home.com
Dec 7 '05 #2
Use type conversion operator in your classes to be converted to
YourString class:

class SomeObject
{
operator YourString() const { /* build YourString from SomeObject
here */ }
};

Note that you need to provide a copy ctor for YourString. The drawback
is that this is less efficient than a YourString(const SomeObject&)
ctor.

Dec 7 '05 #3
Thanks that did the trick! I remember now seeing the conversion
operator before but had forgotten about it. So now I can do assignments
such as:

myString = someObject;

And get the behavior I want without having to explicitly say
someObject.ToString() every time.

Dec 8 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
5
by: Daniel T. | last post by:
The goal is to make a friend function of an inner template class... template < typename T > class Foo { public: class Bar { friend bool operator==( const typename Foo<T>::Bar& lhs, const...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.