473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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.
}
Jul 22 '05 #1
5 1458
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
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.