Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10232
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Subject | Re: index operator overload |
| Date | 2011-09-17 19:32 +0100 |
| Message-Id | <pan.2011.09.17.18.32.48.318000@nowhere.com> |
| Newsgroups | comp.lang.c++ |
| References | <j52e1d$bmt$1@online.de> |
| Organization | Zen Internet |
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.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web