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

When to use operator[]

Hi,

Is it good style to use overload operator[] for a class which isn't
really a container?

Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.

Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?

Thanks,
Darren Grant
Jul 22 '05 #1
7 1274
"Darren Grant" <dg**@hotmail.com> wrote in message
news:op**************@news.hotmail.com...
Hi,

Is it good style to use overload operator[] for a class which isn't
really a container?

Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.
Operator [] implies array semantics. If such semantics makes sense with your
class, why not? Whether your class is actually implemented internally as a
container really doesn't matter.

But if pop[4] is the 4th chromosome, why not call it chromosome[4]?
Logically pop[4] should be the 4th pop, whatever that means.

Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?
Again, you use operator [] to simulate an array, which you can't do with a
floating point argument.

Although array semantics make no sense here, you could use function
semantics -- operator (). Then you would have curve(0.2) returning a 2D
coordinate. I can't tell if it makes sense for your application or not, but
at least the floating point argument wouldn't present a problem.

Thanks,
Darren Grant


--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2
Cy Edmunds wrote:
"Darren Grant" <dg**@hotmail.com> wrote in message
news:op**************@news.hotmail.com...
Hi,

Is it good style to use overload operator[] for a class which isn't
really a container?

Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.

Operator [] implies array semantics. If such semantics makes sense with your
class, why not? Whether your class is actually implemented internally as a
container really doesn't matter.


I agree completely.
But if pop[4] is the 4th chromosome, why not call it chromosome[4]?
Logically pop[4] should be the 4th pop, whatever that means.
I believe "pop" is short for "population." Using [] in this context is
fine.
Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?

Again, you use operator [] to simulate an array, which you can't do with a
floating point argument.


[] is actually used for plenty of things other than simulating arrays.
std::map is a great example. Just make sure you document the interface
concisely and clearly in comments near wherever you declare the operator
overload.
Although array semantics make no sense here, you could use function
semantics -- operator (). Then you would have curve(0.2) returning a 2D
coordinate. I can't tell if it makes sense for your application or not, but
at least the floating point argument wouldn't present a problem.
I personally reserve the () operator (with arg(s)) to indicate that I'm
looking for a particular behavior, rather than retrieving a datum,
although I do see Cy's point. I suppose a good guideline would be that
if you consider your code to be calculating a (potentially changeable)
value based on its argument, use (), and change the name of the function
to include a verb, e.g. "get_curve_at_time." If the return value
depends only on the value of the argument, and can never change, you
probably should use [], but make the name of the object reflect the fact
it behaves like a collection of values; e.g., "curves" instead of "curve."

Thanks,
Darren Grant



Hth,
Jeff

Jul 22 '05 #3
> Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.
This case is fine for overloading [].
Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?


Not sure if C++ allows operator overloading with non-integer index.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Go Beyond UML Use Case and Sequence Diagrams
Jul 22 '05 #4
"EventHelix.com" <ev********@hotmail.com> wrote...
Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.


This case is fine for overloading [].
Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?


Not sure if C++ allows operator overloading with non-integer index.


Yes, it does. Any type will be fine.

Victor
Jul 22 '05 #5
EventHelix.com wrote:
Not sure if C++ allows operator overloading with non-integer index.


Consider operator[] for std::map. In this case, you can index on
any type for which operator< is defined. In general, though,
sure, operator[] can take any type, just like any other class
method. :)
-tom!
Jul 22 '05 #6
On Thu, 25 Dec 2003 20:38:08 -0500, Jeff Schwab <je******@comcast.net>
wrote:
Cy Edmunds wrote:
"Darren Grant" <dg**@hotmail.com> wrote in message
news:op**************@news.hotmail.com...
Hi,

Is it good style to use overload operator[] for a class which isn't
really a container?

Eg, I have a class BezierPop, which represents a population of Bezier
curves (yes, we're talking Genetic Algorithms here). Should I overload
operator[] so we can use pop[4] to refer to the 4th chromosome in the
population, even though BezierPop certainly isn't a value-like class
or a container itself.

Operator [] implies array semantics. If such semantics makes sense with
your
class, why not? Whether your class is actually implemented internally as
a
container really doesn't matter.


I agree completely.
But if pop[4] is the 4th chromosome, why not call it chromosome[4]?
Logically pop[4] should be the 4th pop, whatever that means.


I believe "pop" is short for "population." Using [] in this context is
fine.
Another example, I have a class Bezier, which represents a Bezier
curve (a type of spline - just a hoopy type of curve). I have a member
function which returns a 2D coordinate as a function of t, which is an
interval of [0,1]. Should I overload operator[] so I can do curve[0.2]
or should I make a member function, eg curve.t(0.2) ?

Again, you use operator [] to simulate an array, which you can't do with
a
floating point argument.


[] is actually used for plenty of things other than simulating arrays.
std::map is a great example. Just make sure you document the interface
concisely and clearly in comments near wherever you declare the operator
overload.
Although array semantics make no sense here, you could use function
semantics -- operator (). Then you would have curve(0.2) returning a 2D
coordinate. I can't tell if it makes sense for your application or not,
but
at least the floating point argument wouldn't present a problem.


I personally reserve the () operator (with arg(s)) to indicate that I'm
looking for a particular behavior, rather than retrieving a datum,
although I do see Cy's point. I suppose a good guideline would be that
if you consider your code to be calculating a (potentially changeable)
value based on its argument, use (), and change the name of the function
to include a verb, e.g. "get_curve_at_time." If the return value depends
only on the value of the argument, and can never change, you probably
should use [], but make the name of the object reflect the fact it
behaves like a collection of values; e.g., "curves" instead of "curve."

Thanks,
Darren Grant



Hth,
Jeff


Cool, thanks for your help, everyone.

Darren
Jul 22 '05 #7
"Darren Grant" wrote:
Hi,

Is it good style to use overload operator[] for a class which
isn't really a container?

Eg, I have a class BezierPop, which represents a population
of Bezier curves (yes, we're talking Genetic Algorithms
here). Should I overload operator[] so we can use pop[4] to
refer to the 4th chromosome in the population, even though
BezierPop certainly isn't a value-like class or a container
itself.

Another example, I have a class Bezier, which represents
a Bezier curve (a type of spline - just a hoopy type of
curve). I have a member function which returns a 2D coordinate
as a function of t, which is an interval of [0,1]. Should I
overload operator[] so I can do curve[0.2] or should I make a
member function, eg curve.t(0.2) ?

Thanks, Darren Grant


As others have pointed out, there is no harm in using operator[] for a
non-container class, as long as the meaning of the operator is clear.

However, as a matter of *style*, I usually prefer to use descriptive
member functions. A descriptive name usually goes further to document
and express the meaning of the code. Of course only you can decide if
the meaning of operator[] is clear enough in your domain.

Having said that, my personal opinion is that operator[] is fine for
BezierPop, but I would prefer operator() for the Bezier curve itself.
I tend to associate operator[] with indexing discrete elements, while
operator() is used to evaluate functions (or functors). When you
write curve[0.2], you are not indexing a discrete element, but
evaluating a parametric function, which I would write curve(0.2).
Just my personal opinion.
Jul 22 '05 #8

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

Similar topics

4
by: Asfand Yar Qazi | last post by:
Sorry about this, but its driving me up the wall. First some code: typedef unsigned int size_t; struct AddOp { template<class I1, class I2> static inline float call(size_t i, const I1&...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
9
by: Simon | last post by:
Hi, I have written an ActiveX object to resize images and upload them to a database, this all works fine but when I close internet explorer the process iexporer.exe is still running in my task...
1
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
4
by: Daniel Kraft | last post by:
Hi all, I'd like to know your opinion on when you think overloaded operators should/could be used instead of "ordinary methods". Of course, they are essential for generic programming and there...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.