pointer to pointer conversion

P

pw4getter

Is there any rationale to prohibit implicit conversion from derived** to base** ?
To illustrate my question I submit couple lines below:

class base
{
public:
base(){}
virtual ~base(){}
};
class derived: public base
{
derived(){}
virtual ~derived(){}
};

void do_something(base**ppb)
{}

void test()
{
derived my_d;
derived * pd = &my_d;
do_something(&pd); // Error in VC6,VC.NET And in BC 5;
bse * pb = &my_d;
do_sometning(&pb); // ok, as it should.
}
 
L

Leor Zolman

Is there any rationale to prohibit implicit conversion from derived** to base** ?
To illustrate my question I submit couple lines below:

class base
{
public:
base(){}
virtual ~base(){}
};
class derived: public base
{
derived(){}
virtual ~derived(){}
};

void do_something(base**ppb)
{}

void test()
{
derived my_d;
derived * pd = &my_d;
do_something(&pd); // Error in VC6,VC.NET And in BC 5;
bse * pb = &my_d;
do_sometning(&pb); // ok, as it should.
}

I wasn't clear on why myself, so I did a little research and found this
wonderful article from the CUJ expert's forum:
http://www.cuj.com/documents/s=7995/cujcexp1905hyslop/
I think in there is justification for why that's an error, but I'm going to
have to re-read it myself to remember exactly what the rationale is...
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
V

Victor Bazarov

pw4getter said:
Is there any rationale to prohibit implicit conversion from derived** to
base** ?

Yes. Simple, really. While 'derived' and 'base' are related (implied from
your question and the use of those words as type names) and the conversion
between pointers to them is allowed, derived* and base* are NOT related,
therefore there is no implicit conversion between pointers to them.

Victor
 
N

Nick Hounsome

pw4getter said:
Is there any rationale to prohibit implicit conversion from derived** to base** ?
To illustrate my question I submit couple lines below:

Try the following program:

struct base {int ib};
struct derived : base {int d;}

derived d[2];
derived* dp = d;
derived** dpp = &dp;
base** bpp = reinterpret_cast<base**>(dpp); // force it
base*bp = *bpp;

assert(bp+1 == dp+1); // this will fail
 
A

Andrey Tarasevich

Leor said:
...
I think in there is justification for why that's an error, but I'm going to
have to re-read it myself to remember exactly what the rationale is...
...

At some level of abstraction, the rationale behind this limitation has
the same roots as the well-known limitation that prohibits the following
conversion

char** p = 0;
const char** pc = p; // ERROR

If the original conversion was allowed it would provide a way to
circumvent type-safety rules, just like 'const char**' -> 'char**'
conversion would allow violations of const-correctness rules.
 
C

Casey Carter

Is there any rationale to prohibit implicit conversion from derived** to base** ?

Yes: if you convert a derived** into a base**, then someone could
assign a base* into it that does not point to a derived. e.g.:

derived* d = new derived();
derived** dp = &d;
base** bp = dp; // Implicit conversion
*bp = new base();

After this snippet, **dp is a base, but not a derived.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top