473,372 Members | 1,529 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,372 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 1459
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.