473,394 Members | 1,889 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,394 software developers and data experts.

operator overloading

38
Hi all,

I want to implement an operator to be able to store strings in a queue implicitly through an operator. Something like that:

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. SetString( )
  4.  
  5. {
  6. string1 >> string2 >> string3 >> string4 >> string5
  7.  
  8.  

The operator ">>" should then read each string and store it in a global queue by calling the queue's member function "push()".

Is there any way to implement such operator.

regards
Sep 2 '10 #1
2 870
Banfa
9,065 Expert Mod 8TB
Probably but don't.

That is not the proper semantic meaning of the >> (right shift) operator. I know the stand C++ libraries own stream classes use this operator in this way but IMO there is no good reason to propagate that piece of cannibalisation of an operator.

Just call the queues push function repeatedly. This will also have the effect of making it very clear to anyone reading your code what is happening.

And talking of the queue... don't use global data.


This is one of those cases of the C++ language telling you do things that are all very clever but that in the long run provide little or no advantage but do make you software less maintainable through obfuscation.
Sep 2 '10 #2
Oralloy
988 Expert 512MB
mar11,

I hate to say it, but Banfa has the right of it.

The smart implementation is to build a method that chains:
Expand|Select|Wrap|Line Numbers
  1. inline myQue &append(string const &value)
  2. {
  3.   this.push(value);
  4.   return this;
  5. }
  6.  
Since this chains, you can easily write:
Expand|Select|Wrap|Line Numbers
  1. myQueObject.append(string1).append(string2).append(string3);
  2.  
Of course, if you really want to operator overload, you can:
Expand|Select|Wrap|Line Numbers
  1. inline string &operator >>(string const &value0, string const &value1)
  2. {
  3.   ...
  4. }
  5.  
But beware the maintenence programmer who follows in your footsteps and has to work with your code. The construct you proposed is completely non-obvious. If it's to save a few keystrokes, just say no. Code should be clear, not cryptic.

Also, just as an observation, how do you push a single value? It looks like your construct takes a minimum of two strings.
Sep 2 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
13
by: Jon Cosby | last post by:
VB .Net does support operator overloading, doesn't it? It seems like this should overload binary addition, but VB doesn't recognize "Operator" Public Shared Operator +(ByVal c1 as cnum, ByVal c2...
2
by: vivekian | last post by:
Have a pointer to an object of class type task task * A ; Now this object takes operator overloading like A<<2 ; which assigns the number 2 to one of the members of the object A.
3
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,...
2
by: Bharath | last post by:
Hello All, Can you please let me know if we can do pointer arthrmetic using operator overloading? If not, can you please explain why it's not supported by compiler? I tried below e.g. which was...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also...
8
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,...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
0
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...
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...

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.