 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rune Allnor Guest
|
Posted: Sat Sep 23, 2006 7:43 pm Post subject: Overloading operator[] |
|
|
Hi folks.
I am implementing a class that looks like this:
class MyClass{
private:
int n;
float *f;
public:
:
};
I want to be able to use this class as a regular float array, like
float a,b;
MyClass c;
:
a = c[i];
c[j] = b;
The problem is that I can't see how to declare operator[] and
operator= in order to achieve this.
I am sure this is a standard question, so maybe somebody
here could help me out?
[ For the record: I am at a remote location with no printed C
or C++ documentation available, so no need to go the
RTFM road. I would have consulted the manuals if I had
them here. ]
Rune
{ Moderator's comments:
1. Yes, it's a frequently asked question; see the FAQ section "Operator
overloading", currently at <url:
http://www.parashift.com/c++-faq-lite/operator-overloading.html>.
2. Ordinarily an article that asks a pure FAQ question will be rejected
with reason "FAQ", but the clc++m policy is "when in doubt, accept".
3. Regarding the possibility of harsh RTFM responses, it is in general
quite safe to post to clc++m since it is a moderated group. -mod/aps }
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Frederick Gotham Guest
|
Posted: Sat Sep 23, 2006 9:48 pm Post subject: Re: Overloading operator[] |
|
|
Rune Allnor posted:
| Quote: | I want to be able to use this class as a regular float array, like
float a,b;
MyClass c;
:
a = c[i];
c[j] = b;
The problem is that I can't see how to declare operator[] and
operator= in order to achieve this.#include <cstdlib
|
Perhaps something like:
#include <cassert>
class Coord3D {
private:
float a,b,c;
public:
float const &operator[](unsigned const i) const
{
switch(i)
{
case 0: return a;
case 1: return b;
case 2: return c;
#ifndef NDEBUG
default: assert(i <= 2);
#endif
}
}
float &operator[](unsigned const i)
{
return const_cast<float&>( const_cast<Coord3D const&>(*this)[i] );
}
};
int main()
{
float a=3,b=7,c=2;
Coord3D obj;
obj[2] = c;
a = obj[1];
}
--
Frederick Gotham
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sat Sep 23, 2006 10:08 pm Post subject: Re: Overloading operator[] |
|
|
Rune Allnor wrote:
| Quote: | Hi folks.
I am implementing a class that looks like this:
class MyClass{
private:
int n;
float *f;
public:
:
};
I want to be able to use this class as a regular float array, like
float a,b;
MyClass c;
:
a = c[i];
c[j] = b;
The problem is that I can't see how to declare operator[] and
operator= in order to achieve this.
I am sure this is a standard question, so maybe somebody
here could help me out?
[ For the record: I am at a remote location with no printed C
or C++ documentation available, so no need to go the
RTFM road. I would have consulted the manuals if I had
them here. ]
Rune
{ Moderator's comments:
1. Yes, it's a frequently asked question; see the FAQ section "Operator
overloading", currently at <url:
http://www.parashift.com/c++-faq-lite/operator-overloading.html>.
|
Is it that I'm temporary-blind? Because I don't see the answer to
the OP's question in there ...
{ Simply search the FAQ text for "operator[]" and "operator=" to find
declaration syntax examples. -mod/aps }
| Quote: | 2. Ordinarily an article that asks a pure FAQ question will be rejected
with reason "FAQ", but the clc++m policy is "when in doubt, accept".
|
Nit pick: Ok that when it doubt, accept -- bu if FAQ is a reason for
reject, then why would there be any *doubt* here? (at least under
the -- possibly mistaken? -- premise that the OPs question is answered
in the FAQ listing)
| Quote: | 3. Regarding the possibility of harsh RTFM responses, it is in general
quite safe to post to clc++m since it is a moderated group
|
Well, that's ok for RTFM, but what about RTMF? :-)
Anyway, given that I'm claiming that the question is not answered in
the FAQs (not directly, at least), I might as well volunteer an answer:
1) Operator= has nothing to do with the issue: operator= would apply
to the entire object, and not to the individual floats that are part
of the array.
2) For operator[], the obvious (and mistaken) solution might be:
float operator[] (size_t subscript) const
{
return f[subscript];
}
However, that does not allow you to do obj[0] = 1.5;
The obvious solution to that (still mistaken) is to return a
reference to the float:
float & operator[] (size_t subscript) const
{
return f[subscript];
}
Now client code can use the returned reference to assign to
he referred float. And it works. Sort of... What happens if
you have a *constant* object?? Because the above function is
const (it should be, right? It does not modify any of the
data members of the object), it lets you modify some of the
elements of the array ...
Here, we fall in the physical vs. logical constness issue...
To make a long story short (but you have to promise me that
when you're back from your remote location, you will RTF...
Err, I mean RTMF and look it up in, e.g., Scott Meyers'
Effective C++ book), the generally accepted solution is to
overload the member function on the constness of the object
that invokes it:
float operator[] (size_t subscript) const
// It could also return a const float &
{
return f[subscript];
}
float & operator[] (size_t subscript)
{
return f[subscript];
}
The two execute the same physical action, but they are
logically different -- the non-const version will be
chosen when called for a non-const object, and the const
version whenever you have a const or const-qualified
object.
BTW, it could be better to do it as a template:
template <typename T>
class Array
{
public:
// ...
const T & operator[] (size_t i) const;
T & operator[] (size_t i);
private:
int n;
T * data;
// ...
};
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|