473,378 Members | 1,631 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,378 software developers and data experts.

multi-inherit and pointer to pointer

Here is my code

Class A;
Class B;
Class C : public A, public B
{
....
}

Class D {

C** GetC(){
C** ppC = ...
return ppC;
}
}

Class E{

void Compute(const B* const* ppB){
...
}
}
int main()
{
D d = new D();
C** ppC = d.GetC();

E e;
e.Compute(ppC); //***
}

With VC++ 6.0 the last line with //*** does not compile. If I change that to

e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?

Thanks,

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
10 1603
ho******@yahoo.com wrote:
Here is my code

Class A;
class A {};
Class B;
class B {};
Class C : public A, public B
class C ...
{
...
} ;

Class D {
class D {

public:
C** GetC(){
C** ppC = ...
return ppC;
}
} ;

Class E{
class E {

public:
void Compute(const B* const* ppB){
...
}
}
int main()
{
D d = new D();
C** ppC = d.GetC();

E e;
e.Compute(ppC); //***
}

With VC++ 6.0 the last line with //*** does not compile. If I change that to

e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?


You can't. Conversions 'derived -> base' are only defined for pointers
and references, but not pointers to pointers. Why do you need a pointer
to pointer, anyway? What are you trying to accomplish?

Victor
Jul 22 '05 #2
Victor Bazarov posted:
You can't. Conversions 'derived -> base' are only defined for pointers
and references, but not pointers to pointers.


Interesting. I don't see why it can't be done though.
-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL> wrote in message
news:Qd*****************@news.indigo.ie...
Victor Bazarov posted:
You can't. Conversions 'derived -> base' are only defined for pointers
and references, but not pointers to pointers.


Interesting. I don't see why it can't be done though.


Because it would be dangerous, if it were allowed it would let you treat a
base object as a derived object. It the same reasoning that doesn't let you
convert T** to const T**.

struct B {};
struct D : B { void func(); };

B x;
D* p;
B** q = &p; // conversion from D** to B**, this is not allowed
*q = &x; // now p is pointing at x
p->func();

Converting from D** to B** has allowed us to call func() on x!

This code is adapted from FAQ 18.15

http://www.parashift.com/c++-faq-lit...html#faq-18.15

john
Jul 22 '05 #4
posted:
Here is my code

Class A;
Class B;
Class C : public A, public B
{
...
}

Class D {

C** GetC(){
C** ppC = ...
return ppC;
}
I'd rename this to GetPointerToPointerToC

Why are you using pointers to pointers to objects, as opposed to pointers to
objects?
}

Class E{

void Compute(const B* const* ppB){
...
}
}
int main()
{
D d = new D();
This shouldn't compile. The left operand is of type "D". The right operand
is off type "D* const". It should be either:

D d = *(new D());

D& d = *(new D());

D* p_D = new D();

C** ppC = d.GetC();

E e;
e.Compute(ppC); //***
}

e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?


I myself amn't too familiar with multiple inheritence. But I can assure you
that your above cast is diastrous. You want to supply the function with a
pointer to a pointer to a B object. Using the cast, you're supplying it with
a pointer to a pointer to a C object.

Again I must enquire why the hell you're using pointers to pointers to
objects.
-JKop

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5
On 30 Jun 2004 08:29:23 -0400, ho******@yahoo.com wrote:
Here is my code

Class A;
Since when did class have an uppercase c?
int main()
{
D d = new D();
C** ppC = d.GetC();

E e;
e.Compute(ppC); //***
B* b = *ppC;
e.Compute(&b);
With VC++ 6.0 the last line with //*** does not compile. If I change that to

e.COmpute((const B* const*)ppC);
The data is not passed correctly in the "Compute". How can I fix it?


You can't convert a C** to a B**, so do the above.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6
In article <2d**************************@posting.google.com >,
ho******@yahoo.com writes
Here is my code

Class A;
Class B;
Class C : public A, public B
{
...
}

And there is your problem, base classes have to have been defined, a
(forward) declaration is not enough.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #7
Francis Glassborow <fr*****@robinton.demon.co.uk> writes:
In article <2d**************************@posting.google.com >,
ho******@yahoo.com writes
Here is my code

Class A;
Class B;
Class C : public A, public B
{
...
}

And there is your problem, base classes have to have been defined, a
(forward) declaration is not enough.

[snip]

Maybe not. I suspect the OP's real problem was that a C** couldn't be
converted to a B const*const* . But I can't really tell; the code
he posted was so full of errors - like 'Class', like the one you
point out, etc, that I finally gave up trying to figure out what
the problem was.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #8
JKop <NU**@NULL.NULL> wrote in message
news:<Qd*****************@news.indigo.ie>...
Victor Bazarov posted:
You can't. Conversions 'derived -> base' are only defined for
pointers and references, but not pointers to pointers.


That's not strictly true. I think he forgot the word "implicite". The
standard defines four types of conversions on pointers, and
reinterpret_cast IS allowed for pointers to pointers. Only a subset of
static_cast and const_cast conversions will occur implicitely, however.
Interesting. I don't see why it can't be done though.


And how would you propose implementing it? Keeping in mind that the
address of the base part of an object might not be the same as the
address of the derived part. If I have the following:

Base ** ppb ;
Derived * pd ;
*ppb = pd ;

the compiler knows to adjust the address so that the assigned address
points to the base part of my object. If instead of Base ** ppb, I had
written Derived ** ppd, the compiler would know that no adjustment is
necessary. Except that if *ppd is actuall a Base*, adjustment IS
necessary.

This is why the original poster's example crashed when he used an
explicit conversion. The explicit conversion was a reinterpret_cast,
which lied to the compiler. And when you're talking to a compiler,
lying definitly doesn't pay.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 22 '05 #9
posted:

Interesting. I don't see why it can't be done though.


And how would you propose implementing it? Keeping in mind that the
address of the base part of an object might not be the same as the
address of the derived part. If I have the following:

Base ** ppb ;
Derived * pd ;
*ppb = pd ;

the compiler knows to adjust the address so that the assigned address
points to the base part of my object.


No it doesn't. That's exactly what I'm talking about.

The compiler *can* work with the following:
class Derived : public Base1, public Base2 { ...

Derived derived;

Base2* p_base2 = &derived; //Here it will return what you want - the pointer
to where the Base2 object starts.
But... what doesn't work is:
Derived derived;

Derived* p_derived = &derived;

Base2** = &p_derived;

-JKop

Jul 22 '05 #10
JKop <NU**@NULL.NULL> wrote in message
news:<Jr*****************@news.indigo.ie>...
posted:
Interesting. I don't see why it can't be done though.
And how would you propose implementing it? Keeping in mind that the
address of the base part of an object might not be the same as the
address of the derived part. If I have the following: Base ** ppb ;
Derived * pd ;
*ppb = pd ; the compiler knows to adjust the address so that the assigned
address points to the base part of my object.

No it doesn't. That's exactly what I'm talking about.
But of course it does. This is nothing more than a classical Derived*
to Base* conversion. Surely you aren't saying that a Derived* to Base*
isn't guaranteed to work, or is illegal.
The compiler *can* work with the following: class Derived : public Base1, public Base2 { ... Derived derived; Base2* p_base2 = &derived; //Here it will return what you want - the pointer
to where the Base2 object starts.
So what is the difference compared to my example? Instead of pd (a
Derived*), you use &derived (a Derived*); instead of *ppb (a Base*), you
use p_base2 (a Base*). The types in the assignment expression are the
same. We both assign a Derived* to a Base*.
But... what doesn't work is: Derived derived; Derived* p_derived = &derived; Base2** = &p_derived;


Of course not. That's my point. If you do this, the compiler must be
able to cope with the fact that what the Base2** points to might be a
Derived*. Then, when you assign a Derived* to whatever the Base2**
points to, the compiler has no way of knowing that what the Base2*
points to is really a Derived* (and not a Base2*), so it applies the
fixup, although it shouldn't.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
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
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.