Reference , pointer , constant pointer or what ??

Reference , pointer , constant pointer or what ??

Post by David Curr » Wed, 23 Feb 2005 11:56:55


am confused about references

Consider,

class Apple
{
public:
int i1;
int i2;
Apple(int ii1,int ii2) : i1(ii1), i2(ii2) {}
// ... other methods etc
};

class FruitBox
{
public:
char c1;
Apple& a1;
FruitBox(char cc1, Apple& aa1) : c1(cc1),a1(aa1) {}
// ... etc
};

void FruitBox::useApple(Apple& aa1)
{
a1 = aa1; // CodePosition1 !!
}
// ... etc

void function1(Apple& pApple)
{
Apple stackApple1(1,2);
FruitBox stackFruitBox('a', stackApple1);
// ... some code here
stackFruitBox.useApple(pApple);
// ... some code here
}

void function2( ...)
{
Apple stackApple2(3,4);
// ... code etc
function1(stackApple2);
// ... code etc
}

Now at CodePosition1, stackApple1 contains (3,4).
Naturally this is unacceptable (stackApple1 not out of scope , must be
deconstructed etc).

I now realise that this occurs because FruitBox.a1 is an object and not a
pointer (or a reference).
In FruitBox::useApple the line a1 = aa1 means :-
The (object) Apple a1 (which incidentally is referenced by an address but
that is really none of anyone's business)
is made to be the same as
the (object) Apple aa1 (which also happens to be referenced by an address
and is also none of anyone's business)

So the compiler does a copy from aa1's address to a1's address for the
length of an apple (or similar).

Sadly for me it DOES NOT merely change the address in stackFruitBox.a1
to be the same address of stackApple2 leaving stackApple1 unaltered
This of course is what a pointer would do.

So ...

In my (perhaps limited) 'C' days I would just use a pointer, be very careful
not to ++ it or do
anything silly with the pointer, and be happy that it worked.

I assume the purpose of C++ is to declare what can and cant be done, so
programmers
dont inadvertently make errors, after all this is theoretically what
"private:" is for.

In General

I need a pointer to an object with all facilities (pointer
assignment,methods) EXCEPT pointer increment or decrement.
In other words a pointer to a SINGLE object. To me this is what a
reference implies.

Indeed ...

class Apple
{
public:
int i1;
int i2;
Apple(int ii1,int ii2) : i1(ii1), i2(ii2) {}
void swap(void);
// ... etc
};

void Apple::swap(void)
{
int temp = i2;
i2 = i1;
i1 = temp;
}

void aFunction(void)
{
Apple a1(1,2);
Apple *const cpta = &a1; // a constant pointer to Apple , must be
initialised of course

cpta++; // 23
cpta = &a1; // 24
cpta->swap();


Apple const* pcta = &a1; // a pointer constant to Apple
pcta++;
pcta = &a1;
pcta->swap(); // 31

const Apple* ptac = &a1; // a Pointer to Apple which is constant
ptac++;
ptac= &a1;
ptac->swap(); // 36

Apple *ap = &a1; // a pointer to Apple

for (int i = 0; i < 5 ; i++) // 40
{
ap++;
ap->swap();
ap++;
}
}

gives

[C++ Error] Test.cpp(23): E2024 Cannot modify a const object.
[C++ Error] Test.cpp(24): E2024 Cannot modify a const object.
[C++ Warning] Test.cpp(31): W8037 Non-const function Apple::swap() called
for const object.
[C++ Warning] Test.cpp(36): W8037 Non-const function Apple::swap() called
for const object.

******************************************
 
 
 

Reference , pointer , constant pointer or what ??

Post by maede » Thu, 24 Feb 2005 02:29:32

"David Currie" < XXXX@XXXXX.COM > writes:


Not out of the box, now.

What you can do is define your own type that mimics a built-in
pointer type except pointer arithmetics. Something like

template <typename T>
class MyDecentPointerType
{
public:
MyDecentPointerType();
explicit MyDecentPointerType(T *);

T &operator*() const;
T *operator->() const;

T *get() const;

private:
T *myReferred;
};

You might be tempted to also offer implicit conversion from
MyDecentPointerType<T> to T *, but there are problems to implicit
conversions severe enough that many people prefer to err on the safe
side. This is also why I declared the second constructor explicit.

Of course, you can still construct a MyDecentPointerType object around
a pointer to an array element; but once you have a
MyDecentPointerType, it's hard to inadvertently treat it like a
pointer into an array.

 
 
 

Reference , pointer , constant pointer or what ??

Post by maede » Thu, 24 Feb 2005 04:29:24


XXXX@XXXXX.COM (Thomas Maeder [TeamB]) writes:


<sigh>

Not out of the box, no.

</sigh>