Re: reference trouble in double[3] vs. double*

From: Ali Cehreli (acehreli_at_yahoo.com)
Date: 04/08/04


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



Relevant Pages

  • Re: non-const function return values: gcc bug or language flaw?
    ... > There is a good reason for the cannot bind temporary to a non-const ... data members mutable and all its member functions const, ... So if the compiler is already able to do all the non-const things that I ... declaration before it does? ...
    (comp.lang.cpp)
  • Re: reference trouble in double[3] vs. double*
    ... double operator(unsigned int i) const ... Note the different return types for const vs. non-const. ...
    (comp.lang.cpp)
  • Re: Re-use the argument?
    ... using of const variables when it knows they're no longer needed. ... a trivial calculation into three separate statements. ... function parameter as an ordinary modifiable object. ... anyone who looks at the function declaration. ...
    (comp.lang.c)
  • Re: Simple question on Pointers
    ... Such a member is, of course, bad idea. ... const SomeClass, this method exhibits undefined behavior, but you won't ... your completely unambiguous declaration that m_iMember is, in fact, constant ...
    (microsoft.public.vc.language)
  • Re: Use of the Volatile keyword for a pointer to a volatile memory block
    ... > volatile', as they are basically handled the same way) always appear after ... > leftmost side of a declaration they apply to the thing on their right. ... > volatile pointer to an const double you would use ... specifiers aren't saying that we have a double const (what's that, ...
    (comp.lang.c)

Loading