index operator overload

P

Philipp Kraus

Hello,

I would like to overload in my class the [] or () operator. Many
tutorials create the call like
myvaluetype& operator[] (const int&)
or
myvaluetype operator[] (const int&)

but is this correct? An index is in my opinion >= 0, so I should use a
uint like std::size_t .
The call can be also const, because it returns a value with no modify
of object data.

Is there a coding style guide or anything else for a correct call? I
would like to create
my overloading call to:
myvaluetype operator[] (const std::size_t&) const;

Thanks

Phil
 
M

Marc

Philipp said:
I would like to overload in my class the [] or () operator. Many
tutorials create the call like
myvaluetype& operator[] (const int&)

Don't pass builtin types (like int) by reference but by value instead.
or
myvaluetype operator[] (const int&)

This one should likely have a "const" at the end.
but is this correct? An index is in my opinion >= 0, so I should use a
uint like std::size_t .

It is a matter of taste that gives rise to a lot of trolling from both
sides.
The call can be also const, because it returns a value with no modify
of object data.

Note that the first version you wrote above returns a non-const
reference, so it enables you to modify the object and can't be const.
Is there a coding style guide or anything else for a correct call? I
would like to create
my overloading call to:
myvaluetype operator[] (const std::size_t&) const;

* pass size_t by value
* overload sort of implies there are 2 versions. Where is the other
one?
 
N

Noah Roberts

Philipp Kraus  wrote:
Is there a coding style guide or anything else for a correct call? I
would like to create
my overloading call to:
myvaluetype operator[] (const std::size_t&) const;

* pass size_t by value
* overload sort of implies there are 2 versions. Where is the other
  one?

When you're talking about "operator overloading" the "other one" is
the standard operator.
 
N

Nobody

I would like to overload in my class the [] or () operator. Many tutorials
create the call like
myvaluetype& operator[] (const int&)
or
myvaluetype operator[] (const int&)

but is this correct? An index is in my opinion >= 0, so I should use a
uint like std::size_t .

The STL containers which provide operator[] use an unsigned type.
The call can be also const, because it returns a value with no modify of
object data.

The first one above returns a non-const reference, so it can't be const.
Is there a coding style guide or anything else for a correct call? I would
like to create
my overloading call to:
myvaluetype operator[] (const std::size_t&) const;

If the objects actually exist within the container (rather than being
generated on the fly), you should normally return a reference rather than
a copy.
 
J

James Kanze

I would like to overload in my class the [] or () operator.
Many tutorials create the call like
myvaluetype& operator[] (const int&)
or
myvaluetype operator[] (const int&)
but is this correct?

It depends. The latter should almost certainly have a 'const'
at the end of it, and it's quite exceptional to pass int by
reference.
An index is in my opinion >= 0, so I should use a
uint like std::size_t .

First, there's nothing which says an index can't be negative;
I've written classes where it was. And secondly, you have to
consider the global context: if the indexes are numeric, it
makes sense to take the difference between two indexes, using
the formula 'abs(index1 - index2)'; if this expression doesn't
work, the index doesn't have an appropriate type.

Ideally, of course, C++ would have subrange types, which could
only hold the value [lower_bound...upper_bound) (but whose
subtraction would result in the correct value, probably of type
int). C++ doesn't, however, so we make do with int.
The call can be also const, because it returns a value with no modify
of object data.
Is there a coding style guide or anything else for a correct call?

The practices in "Programming: Principles and Practice using
C++" would be a good starting point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top