Re: reference trouble in double[3] vs. double*
From: Ali Cehreli (acehreli_at_yahoo.com)
Date: 04/08/04
- Next message: red floyd: "Re: reference trouble in double[3] vs. double*"
- Previous message: Claudio Puviani: "Re: Stable priority queue"
- In reply to: Jacek Dziedzic: "reference trouble in double[3] vs. double*"
- Next in thread: red floyd: "Re: reference trouble in double[3] vs. double*"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 7 Apr 2004 15:17:38 -0700
Jacek Dziedzic <jacek__NOSPAM__@janowo.net> wrote in message news:<c51its$551$1@korweta.task.gda.pl>...
> Hi!
>
> Let's say I have a class called Triplet that serves as an envelope
> for double[3], ie.
>
> class Triplet {
> public:
> Triplet() {/*...*/}
> /*
> some things that double[3] doesn't have, like
> a << operator to send it to a stream
> */
You should have a declaration in the commented-out section like this:
double const & operator[] (unsigned int i) const;
Please note that a const member function cannot return a non-const
reference. For that reason, you must declare the return type as
'double' or 'double const &'.
Also note that the 'const' for the argument is not the part of the
signature but an implementation detail, because the 'i' that users
pass will be copied to the function.
For that reason, some argue that it shouldn't take part in the
interface. It doesn't matter really because that top-level const
doesn't take part in the signature of the function.
If you need to provide non-const access too, then you can define the
non-const version:
double & operator[] (unsigned int i);
>
> private:
> double storage[3];
> };
>
> I'm having a problem with the subscript operator, I tried
>
> double& Triplet::operator[](const unsigned int i) const {
> return storage[i];
> }
To match the declaration above, the return type must be 'double const
&' here. Cont-qualifying 'i' is ok here because this is the
implementation.
Ali
- Next message: red floyd: "Re: reference trouble in double[3] vs. double*"
- Previous message: Claudio Puviani: "Re: Stable priority queue"
- In reply to: Jacek Dziedzic: "reference trouble in double[3] vs. double*"
- Next in thread: red floyd: "Re: reference trouble in double[3] vs. double*"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|