Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Could you explain cBase*& in this example code?

40 views
Skip to first unread message

fl

unread,
Jul 17, 2015, 11:30:24 AM7/17/15
to
Hi,

I find the following code snippet on line. I feel that is interesting for my
learning.

This part is difficult to understand for me.

cBase*& operator[](unsigned index)
{
// much more complicated, but simplified for example
return array[index];
}

Could you explain it to me?
'*&' looks weird I feel.

Thanks,



.......
// the classes
class cBase {};
class cDerived : public cBase {};

class cBaseArray
{
// the array of pointers to cBase
cBase** array;

// overloaded operator that returns an element of the array
cBase*& operator[](unsigned index)
{
// much more complicated, but simplified for example
return array[index];
}
};

Bo Persson

unread,
Jul 17, 2015, 11:34:50 AM7/17/15
to
On 2015-07-17 17:30, fl wrote:
> Hi,
>
> I find the following code snippet on line. I feel that is interesting for my
> learning.
>
> This part is difficult to understand for me.
>
> cBase*& operator[](unsigned index)
> {
> // much more complicated, but simplified for example
> return array[index];
> }
>
> Could you explain it to me?
> '*&' looks weird I feel.
>

It's just a reference, nothing odd.

Below you have Base** which is a pointer to a pointer to Base.
Similarly, Base*& is a reference to a pointer to Base.

Barry Schwarz

unread,
Jul 17, 2015, 2:32:01 PM7/17/15
to
On Fri, 17 Jul 2015 08:30:13 -0700 (PDT), fl <rxj...@gmail.com>
wrote:

>Hi,
>
>I find the following code snippet on line. I feel that is interesting for my
>learning.
>
>This part is difficult to understand for me.
>
> cBase*& operator[](unsigned index)

Does it make sense to you if the & were not there. The function would
return a value. That value would have type "pointer to cBase." By
way of example, strcpy also returns a value. That value has type
pointer to char.

Now put the & back in. Now the function returns a reference. That
reference "refers" to an object of type "pointer to cBase".

> {
> // much more complicated, but simplified for example
> return array[index];

Obviously, the reference being returned refers to the object
array[index].

> }
>
>Could you explain it to me?
>'*&' looks weird I feel.
>
>Thanks,
>
>
>
>.......
>// the classes
>class cBase {};
>class cDerived : public cBase {};
>
>class cBaseArray
>{
> // the array of pointers to cBase
> cBase** array;

array has type cBase**. This means that cBase points to an object of
type cBase*. From the context, it appears obvious the object cBase
points to is followed in memory by several more objects of the same
type. Normal array notation allows array[0] to designate the first of
these objects, array[1] the second, etc. array[index] designates one
of these objects, which one being determined at run time.

> // overloaded operator that returns an element of the array
> cBase*& operator[](unsigned index)
> {
> // much more complicated, but simplified for example
> return array[index];

Since all the objects pointed to by array have type cBase*, object
array[index] has this type. The reference that is returned designates
this object. This allows the calling function to use this reference
to access the cBase object array[index] points to. It also allows the
calling function to change the value in array[index] itself so that it
points to some other object of type cBase.

If the & were missing, the calling function would receive the value of
array[index] and could access the cBase object pointed to but could
not change the value of array[index] itself.

Whether the calling function should be allowed to change a value in
one of the array objects is a different question.

> }
>};

--
Remove del for email

Paul N

unread,
Jul 18, 2015, 12:45:03 PM7/18/15
to
On Friday, 17 July 2015 16:30:24 UTC+1, fl wrote:
> Hi,
>
> I find the following code snippet on line. I feel that is interesting for my
> learning.
>
> This part is difficult to understand for me.
>
> cBase*& operator[](unsigned index)
> {
> // much more complicated, but simplified for example
> return array[index];
> }
>
> Could you explain it to me?
> '*&' looks weird I feel.

Here's a simpler example. Suppose we have a function that returns a value. But it might also go wrong. We start off with a pointer to char (a C-style string) and if the function works this is left as NULL and the returned value can be used. If it is not NULL, the returned value should not be used, and the pointer can be used, say in an error message.

int func(char *& err, ...other stuff...) {
if (problem) { err = "First problem"; return 0; }
stuff here
if (other problem) { err = "Second problem"; return 0; }
return value; } // zero might be a valid value here

int caller(stuff) {
char *err = NULL;
val = func(err, whatever);
if (err) { moan about error }
else { use val } }
0 new messages