Topic: Reference to base class pointer
Author: Ewert_Ahr._Electronic_GmbH@t-online.de (Gernot)
Date: 1999/05/19 Raw View
I've just changed some existing code and abandonded using pointers to
pointers. Instead I'm using references to pointers now, which greatly
improves readability. But still my compiler keeps telling me that it
cannot cast a reference to a pointer to class Derived to a reference to
a Base class pointer.
Simple question: Is that standard-conforming behaviour or can I hope for
a new compiler version?
Just for clarity, the essential code:
class Base
{
};
class Derived : public Base
{
};
void foo( Base*& rpBase );
int main()
{
Derived* pDerived = NULL;
foo( pDerived ); // This is the error!
...
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "news.xs4all.nl" <sbierman@idfix.nl>
Date: 1999/05/20 Raw View
The same problem holds for converting a Derived ** to a Base **. This is not
allowed for the following reason:
class Base { };
class Derived1 : public Base { };
class Derived2 : public Base { };
void foo()
{
Derived1 d1;
Derived1 *p1 = &d1;
Derived2 d2;
Derived2 *p2 = &d2;
Base **pp = &p1; /* convert Derived1** to Base**, not allowed */
*pp = p2; /* convert Derived2* to Base *, allowed */
/* At this point p1 which is a
Derived1 pointer is pointing to
a Derived2 variable.
Possible disaster!!
*/
}
Your example is simply a variation of the same principle.
Silvio Bierman
Gernot wrote in message <37428FB3.5DC7@eae.com>...
>I've just changed some existing code and abandonded using pointers to
>pointers. Instead I'm using references to pointers now, which greatly
>improves readability. But still my compiler keeps telling me that it
>cannot cast a reference to a pointer to class Derived to a reference to
>a Base class pointer.
>Simple question: Is that standard-conforming behaviour or can I hope for
>a new compiler version?
>
>Just for clarity, the essential code:
>
>class Base
>{
>};
>
>class Derived : public Base
>{
>};
>
>void foo( Base*& rpBase );
>
>int main()
>{
> Derived* pDerived = NULL;
> foo( pDerived ); // This is the error!
>...
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/05/20 Raw View
Gernot wrote:
>
> I've just changed some existing code and abandonded using pointers to
> pointers. Instead I'm using references to pointers now, which greatly
> improves readability. But still my compiler keeps telling me that it
> cannot cast a reference to a pointer to class Derived to a reference to
> a Base class pointer.
> Simple question: Is that standard-conforming behaviour or can I hope for
> a new compiler version?
>
> Just for clarity, the essential code:
>
> class Base
> {
> };
>
> class Derived : public Base
> {
> };
>
> void foo( Base*& rpBase );
>
> int main()
> {
> Derived* pDerived = NULL;
> foo( pDerived ); // This is the error!
> ...
I think this is standard-conforming behaviour. (It definitely should be)
Look at the following program:
class Base
{
public:
virtual ~Base() {}
};
class Derived:
public Base
{
public:
int i;
};
void foo(Base*& rpBase);
int main()
{
Derived* pDerived = NULL;
foo(pDerived);
if (pDerived)
pDerived->i=5;
delete pDerived;
}
void foo(Base*& rpBase)
{
tpBase = new Base;
}
If the call would be allowed, you would get the following:
- main calls foo, handing it out the pDerived
- foo assigns a pointer to Base to its argument (that's OK, since
it's a reference to pointer to Base)
- after return, main checks that the pointer has been assigned
(of course it has), and therefore concludes that it can use it
to access the i of the Derived object it points to
- But: It really points to a Base, there's no i at all in the Base,
and anything may happen! (Think of the even worse case where
foo had assigned another derived class to rpBase - you could
overwrite arbitrary data)
BTW, if the code had worked with Base** and &pDerived, then your
compiler is broken.
Or, to make the formal argument:
Base* and Derived* are completely unrelated, except that a
conversion from Derived* to Base* exists. So your code shouldn't
compile for the same reason for which the following doesn't
compile:
void foo(int& a);
int main()
{
char x;
foo(x);
}
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]