TWiki . Edumacation . CPlusPlusLessons TWiki . { Main | Edumacation | TWiki | Test }
Edumacation . { Home | Users | Changes | Search | Go }

Using the subscript operator with pointers.

The subscript operator normally is used like this:

Foo bar[100];
int plugh = 37;
Foo xyzzy = bar[plugh];

So what if you've declared things this way instead?

Foo *bar = new Foo[100];
int plugh = 37;

You might like to try:

Foo xyzzy = bar->[plugh];

but the compiler will not hear such nonsense.

However, it will accept the following nonsense:

Foo xyzzy = bar->operator[](plugh);

This is perverse, however, because the main point of overloading operators was to have a notation that created a suspension of disbelief, i.e. to make the reader believe that the [] actually is a subscript and not some arbitrary function. The operator[] destroys that suspension of disbelief, bringing one back to the reality that the you really don't know what the subscript operator is doing unless you check the code.

The [] notation may only be used with objects and references to objects. For reasons to be explained later, pointers do not have equal standing.

Here's one approach to working around this problem:

Foo *bar = new Foo[100];
Foo refBar& = bar;
int plugh = 37;
Foo xyzzy = refBar[plugh];

However, this is not always satisfactory; sometimes, you need a pointer. Instead, try this:

Foo *bar = new Foo[100];
int plugh = 37;
Foo xyzzy = (*bar)[plugh];

It's apparent right from first sight that reference notation is nothing more than syntactic sugar for pointers. In fact, if you look up "syntactic sugar" in a dictionary, the example will be the use of subscript notation in lieu of pointer arithmetic (really!).

Here's another explanation from :

We use the C++ const reference notation for speed, the C pointer notation to let a function change the value of its arguments.

No doubt, the above is the perspective of a C programmer newly-introduced to C++ who's wondering why this redundant notation was inroduced. References are pointers that generally have the syntax and semantics of objects. References are what they are, and they do what they do. References are generally the preferered way to receive objects. They give you both speed and the ability to modify the object passed; this ability can be disabled with the appropriate declaration.

References are implcitly const.

Why should reference implicitly be const? Here's the explanation:

If references could be modified, then they'd be pointers.

I didn't claim it would be a good explanation, but here's the more plausible one:

References are generally introduced in the context of function arguments. In this context, they are in fact const, just like any other function arguments. It appears that the C++ language designers simply made a boo boo, generalizing this restriction to the use of references in any context.

use transform for upper/lower-casing strings

Topic CPlusPlusLessons . { Edit | Ref-By | Attach | Diffs | r1.2 | > | r1.1 }
You must register before editing pages or using other extended features on this TWiki system.
Revision r1.2 - 02 Oct 2004 - 19:27 by EliMantel web search for EliMantel
Privacy Policy
Copyright © 2000-2005 by the contributing authors. All material on this collaboration tool is the property of the contributing authors. Collect email addresses here.
Ideas, requests, problems regarding TWiki? Send feedback.