Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10225 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| First post | 2011-09-17 17:23 +0200 |
| Last post | 2011-09-20 21:50 +0200 |
| Articles | 8 — 6 participants |
Back to article view | Back to comp.lang.c++
index operator overload Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-17 17:23 +0200
Re: index operator overload Marc <marc.glisse@gmail.com> - 2011-09-17 16:18 +0000
Re: index operator overload Noah Roberts <roberts.noah@gmail.com> - 2011-09-17 09:53 -0700
Re: index operator overload Marc <marc.glisse@gmail.com> - 2011-09-17 18:22 +0000
Re: index operator overload Nobody <nobody@nowhere.com> - 2011-09-17 19:32 +0100
Re: index operator overload James Kanze <james.kanze@gmail.com> - 2011-09-19 15:31 -0700
Re: index operator overload Leigh Johnston <leigh@i42.co.uk> - 2011-09-19 23:41 +0100
Re: index operator overload Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-09-20 21:50 +0200
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2011-09-17 17:23 +0200 |
| Subject | index operator overload |
| Message-ID | <j52e1d$bmt$1@online.de> |
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
[toc] | [next] | [standalone]
| From | Marc <marc.glisse@gmail.com> |
|---|---|
| Date | 2011-09-17 16:18 +0000 |
| Message-ID | <j52h8j$drb$1@news-rocq.inria.fr> |
| In reply to | #10225 |
Philipp Kraus wrote: > 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?
[toc] | [prev] | [next] | [standalone]
| From | Noah Roberts <roberts.noah@gmail.com> |
|---|---|
| Date | 2011-09-17 09:53 -0700 |
| Message-ID | <066b51ae-c882-4aa7-8d57-f5e4141d2061@d26g2000yqa.googlegroups.com> |
| In reply to | #10227 |
On Sep 17, 9:18 am, Marc <marc.gli...@gmail.com> wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Marc <marc.glisse@gmail.com> |
|---|---|
| Date | 2011-09-17 18:22 +0000 |
| Message-ID | <j52ohi$h4v$1@news-rocq.inria.fr> |
| In reply to | #10230 |
Noah Roberts wrote: > When you're talking about "operator overloading" the "other one" is > the standard operator. Ah, yes, it makes sense, thanks.
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-09-17 19:32 +0100 |
| Message-ID | <pan.2011.09.17.18.32.48.318000@nowhere.com> |
| In reply to | #10225 |
On Sat, 17 Sep 2011 17:23:25 +0200, Philipp Kraus wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | James Kanze <james.kanze@gmail.com> |
|---|---|
| Date | 2011-09-19 15:31 -0700 |
| Message-ID | <ef1261a1-24c9-46fd-91d6-0619d04fced7@s16g2000yqc.googlegroups.com> |
| In reply to | #10225 |
On Sep 17, 4:23 pm, Philipp Kraus <philipp.kr...@flashpixx.de> wrote: > 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. -- James Kanze
[toc] | [prev] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-09-19 23:41 +0100 |
| Message-ID | <HPydnWMDnrW0WOrTnZ2dnUVZ8oGdnZ2d@giganews.com> |
| In reply to | #10304 |
On 19/09/2011 23:31, James Kanze wrote: > On Sep 17, 4:23 pm, Philipp Kraus<philipp.kr...@flashpixx.de> wrote: > >> 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. Using std::size_t for indexes is perfectly fine; std::vector's index type is std::size_t by default. > > 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. Who says? std::size_t is fine for indexes which are *usually* non-negatvie. /Leigh
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2011-09-20 21:50 +0200 |
| Message-ID | <j5aqqh$oup$1@online.de> |
| In reply to | #10225 |
Thanks a lot to all for your ansers Phil
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web