By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

pointer to pointer conversion

P: n/a
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.
}
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 5 Mar 2004 07:06:10 -0800, pw*******@yahoo.com (pw4getter) wrote:
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
le**@bdsoft.com
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
Jul 22 '05 #2

P: n/a
"pw4getter" <pw*******@yahoo.com> wrote...
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
Jul 22 '05 #3

P: n/a

"pw4getter" <pw*******@yahoo.com> wrote in message
news:79*************************@posting.google.co m...
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
Jul 22 '05 #4

P: n/a
Leor Zolman wrote:
...
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5

P: n/a
pw*******@yahoo.com (pw4getter) wrote in message news:<79*************************@posting.google.c om>...
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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.