Topic: [] operator on pointers.


Author: Edward Diener <eddielee@abraxis.com>
Date: 1999/03/14
Raw View
Ivan Strougatski wrote:

> I got a class with overloaded [] operator.
> In VC (5.0) I can not apply it pointers to a class
> /*
>         template<typename t> class c{
>                 ....
>                 t* operator[](int i);
>                 ....
>         }
>
>         c<something>* l;
>         .....
>         something* s = l[3]; // compilers error here

Try

something* s = (*l)[3];

instead since "l" is a pointer to your template class.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/03/14
Raw View
In article <36EAFD0B.1EBAF005@wmin.ac.uk>, tpkec@wmin.ac.uk says...
> I got a class with overloaded [] operator.
> In VC (5.0) I can not apply it pointers to a class
> /*
>  template<typename t> class c{
>   ....
>   t* operator[](int i);
>   ....
>  }
>
>  c<something>* l;
>  .....
>  something* s = l[3]; // compilers error here
>  something* s2 = l->operator[](3) // this has to be used instead
> */
>
> Is this error in compiler of "feature" in Standard C++.

This is correct behavior.  You've overloaded operator[] for instances
of the class itself.  You're creating a pointer TO the class, not an
instance OF the class.  Using the framework you supplied:

 c<something> *L; // avoid 'l'...

 L = new c<something>[10];

now, L[3] (for example) refers to the third object in the array
pointed to by 'L', which was allocated with new.  Since that produces
a reference to an object which overloads operator[], you could
perfectly reasonably use something like `L[3][5]'.  The first would
dereference the pointer, giving you an object.  The second would
invoke operator[] on that object, returning (in this case) a t*.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/15
Raw View
Ivan Strougatski wrote:
>
> I got a class with overloaded [] operator.
> In VC (5.0) I can not apply it pointers to a class
> /*
>         template<typename t> class c{
>                 ....
>                 t* operator[](int i);
>                 ....
>         }
>
>         c<something>* l;
>         .....
>         something* s = l[3]; // compilers error here
>         something* s2 = l->operator[](3) // this has to be used instead
> */
>
> Is this error in compiler of "feature" in Standard C++.

You can't overload the behavior of operators on structure pointers, only
of operators on structures themselves. l[3] is defined to *(l+3), and
l+3 is an invalid pointer value. You want (*l)[3].


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ivan Strougatski <tpkec@wmin.ac.uk>
Date: 1999/03/15
Raw View
Thanks to everyone who answered my enquiry.
I have to admit that after programing for a while in C++,
it looks to me that C++ discourages to use dynamic
(reference by B. Meyer notation) objects.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/15
Raw View
Ivan Strougatski wrote:
>
> Thanks to everyone who answered my enquiry.
> I have to admit that after programing for a while in C++,
> it looks to me that C++ discourages to use dynamic
> (reference by B. Meyer notation) objects.

Three additional characters is sufficient to discourages their use? I
suspect you can even arrange to drop those three characters, by using a
reference or a copy of the object itself, rather than a pointer to it,
but whether such a change is feasible depends upon the context.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Ivan Strougatski <tpkec@wmin.ac.uk>
Date: 1999/03/14
Raw View
I got a class with overloaded [] operator.
In VC (5.0) I can not apply it pointers to a class
/*
 template<typename t> class c{
  ....
  t* operator[](int i);
  ....
 }

 c<something>* l;
 .....
 something* s = l[3]; // compilers error here
 something* s2 = l->operator[](3) // this has to be used instead
*/

Is this error in compiler of "feature" in Standard C++.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]