Topic: Reference primitives in C++


Author: denis@bull.ilyichevsk.odessa.ua (Denis Vlasenko)
Date: 1997/09/14
Raw View
Hi.

> > 'True' references, such as those found in Java, Eiffel, and Smalltalk, can
> > be modified freely (to point to other objects) and can also be set to
> > 'void' or 'null' (meaning that they don't point to any object).  C++ can't
> > do this; it has no concept of a 'void' or modifiable reference.
>
> I'm not familiar with those languages. How does a 'true' reference
> differ from a C++ pointer (ignoring purely syntactical details such as
> '&', '*', and '->')? What can you do with a 'true' reference that cannot
> be done with a C++ pointer?

1. Operators can be overloaded for user-defined classes or refs to
   them, but not for a ptrs:
   MyClass operator+ (const MyClass&, const MyClass&);  // ok
   MyClass operator+ (const MyClass*, const MyClass&);  // ok
   MyClass operator+ (const MyClass&, const MyClass*);  // ok
   MyClass operator+ (const MyClass*, const MyClass*);  // err

2. Copy ctor/op= are defined as ctor/op= with const ref to the object
   of the same type as a parameter. Without references it would be a
   ptr to const object. Imagine: instead of a = b you should write
   a = &b!

3. You're unable to take ptr to object when op& is overloaded for its
   class. You always can take reference.

Am I forgot something more?
--
Denis Vlasenko
Mail:  13-104 K.Marx street, Ilyichevsk, Odessa region, 270901 Ukraine
Phone: (+380-48) 686-8134
Email: denis@bull.ilyichevsk.odessa.ua
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1997/09/15
Raw View

Denis Vlasenko wrote:
>
> Hi.
>
> > > 'True' references, such as those found in Java, Eiffel, and Smalltalk, can
> > > be modified freely (to point to other objects) and can also be set to
> > > 'void' or 'null' (meaning that they don't point to any object).  C++ can't
> > > do this; it has no concept of a 'void' or modifiable reference.
> >
> > I'm not familiar with those languages. How does a 'true' reference
> > differ from a C++ pointer (ignoring purely syntactical details such as
> > '&', '*', and '->')? What can you do with a 'true' reference that cannot
> > be done with a C++ pointer?
>
> 1. Operators can be overloaded for user-defined classes or refs to
>    them, but not for a ptrs:
>    MyClass operator+ (const MyClass&, const MyClass&);  // ok
>    MyClass operator+ (const MyClass*, const MyClass&);  // ok
>    MyClass operator+ (const MyClass&, const MyClass*);  // ok
>    MyClass operator+ (const MyClass*, const MyClass*);  // err
>
> 2. Copy ctor/op= are defined as ctor/op= with const ref to the object
>    of the same type as a parameter. Without references it would be a
>    ptr to const object. Imagine: instead of a = b you should write
>    a = &b!
>
> 3. You're unable to take ptr to object when op& is overloaded for its
>    class. You always can take reference.

All of your answers refer to C++ references. My question was not about
those, but about the so-called 'true' references described in the first
paragraph. They don't exist in C++, but sound suspiciously similar in
function to C/C++ pointers, with the added syntactic sugar of not
requiring the use of '*', '&', or '->'.

The question came up become some people want C++ references changed into
'true' references. If that were done, the three items you mentioned
would be just a few of hundreds of details that would need to be
reconsidered.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]