Topic: this" as a reference (was: C++ Scandal?!)


Author: "peter koch" <peter.koch.larsen@gmail.com>
Date: Thu, 6 Apr 2006 18:00:19 CST
Raw View
Seungbeom Kim skrev:

> John Nagle wrote:
> >
> >    A few of the legacy problems from early C++:
> >
> >     - References went in late, and many things that should
> >     have been references are pointers.  "this", for example.
>
> I have heard this mentioned often, but if this were a reference,
> how would you get around overloaded operators such as operator&()
> or operator->()?
>
I do not see the relation between these items. Implementing e.g.
operator->() is entirely unrelated to whether this is of type T& or of
type T*.

/Peter
> Seungbeom Kim
>

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Seungbeom Kim <musiphil@bawi.org>
Date: Tue, 4 Apr 2006 15:47:52 CST
Raw View
John Nagle wrote:
>
>    A few of the legacy problems from early C++:
>
>     - References went in late, and many things that should
>     have been references are pointers.  "this", for example.

I have heard this mentioned often, but if this were a reference,
how would you get around overloaded operators such as operator&()
or operator->()?

--
Seungbeom Kim

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: wade@stoner.com
Date: 5 Apr 2006 15:40:02 GMT
Raw View
Seungbeom Kim wrote:
> if 'this' were a reference,
> how would you get around overloaded operators such as operator&()
> or operator->()?

T* AddressOf(T& x)
{
  char& c = reinterpret_cast<char&>(x);
  return reinterpret_cast<T*>(&c);
}

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: NULL@NULL.NULL ("Tom s")
Date: Wed, 5 Apr 2006 17:23:45 GMT
Raw View
> T* AddressOf(T& x)
> {
>   char& c =3D reinterpret_cast<char&>(x);
>   return reinterpret_cast<T*>(&c);
> }


What's that?

Are you trying to write a function which will return the address of an=20
object which is passed to it by reference?

template<class T>
T* AddressOf(T& object)
{
    return &object;
}


It's undefined behaviour to use reinterpret_cast change one pointer type=20
to another. You can only change to and from "void*".


-Tom=E1s

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]