Subj : Reference , pointer , constant pointer or what ?? To : borland.public.cpp.borlandcpp From : David Currie Date : Tue Feb 22 2005 01:56 pm I 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. **************************************************************************** ******* Of COURSE I can always just use a pointer. Consider the absolute ABSURDITY of the (perfectly legal) code at 40 - 45. I'm not saying programmers are stupid or malicious, but there are other more subtle bugs (undetected by a compiler) that can occur even after great care. Protection at no cost to facility is always desirable. In C, it is assumed programmers know what they are doing and so you can do anything, there is very little protection. Presumably in C++ , there is greater protection but restriction on what you can do. My Question is this Is there pointer to a SINGLE object(No Incr,Decr) available in C++. If so what is it. If not must I therefore use a pointer and take my chances. I would be grateful for any response. David .