 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
pw4getter Guest
|
Posted: Fri Mar 05, 2004 3:06 pm Post subject: pointer to pointer conversion |
|
|
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.
}
|
|
| Back to top |
|
 |
Leor Zolman Guest
|
Posted: Fri Mar 05, 2004 3:40 pm Post subject: Re: pointer to pointer conversion |
|
|
On 5 Mar 2004 07:06:10 -0800, [email]pw4getter (AT) yahoo (DOT) com[/email] (pw4getter) wrote:
| Quote: | 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
[email]leor (AT) bdsoft (DOT) com[/email]
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Mar 05, 2004 3:42 pm Post subject: Re: pointer to pointer conversion |
|
|
"pw4getter" <pw4getter (AT) yahoo (DOT) com> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Nick Hounsome Guest
|
Posted: Fri Mar 05, 2004 7:09 pm Post subject: Re: pointer to pointer conversion |
|
|
"pw4getter" <pw4getter (AT) yahoo (DOT) com> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Fri Mar 05, 2004 9:49 pm Post subject: Re: pointer to pointer conversion |
|
|
Leor Zolman wrote:
| Quote: | ...
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
|
|
| Back to top |
|
 |
Casey Carter Guest
|
Posted: Fri Mar 05, 2004 11:41 pm Post subject: Re: pointer to pointer conversion |
|
|
[email]pw4getter (AT) yahoo (DOT) com[/email] (pw4getter) wrote in message news:<7914aaeb.0403050706.5c91f6e (AT) posting (DOT) google.com>...
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|