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

upcasting pointer-to-pointer-to-class

Hi there everyone

I've got:

class A
{ ... };

class B : public A
{ ... }

void foo(A** a)
{ ... }

int main(...)
{
B* b = new B();
foo(&b);
}

GCC complains with "passing 'B**' as argument 1 of 'foo(A**)'"

Upcasting through one level of reference is most common, why is this different?

Thanks for any insight,

Judith
Jul 19 '05 #1
2 5265

"Judith" <ju*****@inet.co.za> wrote in message news:4d**************************@posting.google.c om...

GCC complains with "passing 'B**' as argument 1 of 'foo(A**)'"

Upcasting through one level of reference is most common, why is this different?

Didn't we just answer this (look around on the group).

class Base { };
class Derived : public Base { };

Base b, *bp, **bpp;
Derived d, *dp, **dpp;

dp = &d;
bp = dp; // fine, we know how to convert from Derived* to Base* and can do so in this operation.
bpp = &dpp; // we convert from Derived** to Base** ok, but how does the value *dpp get converted
// to *bpp?

It's not a safe conversion because the compiler at the time *bpp is used doesn't know
what conversion might be needed to apply to the value to make it really a Base* value.
Jul 19 '05 #2
On 17 Nov 2003 07:48:14 -0800, ju*****@inet.co.za (Judith) wrote:
Hi there everyone

I've got:

class A
{ ... };

class B : public A
{ ... }

void foo(A** a)
{ ... }

int main(...)
{
B* b = new B();
foo(&b);
}

GCC complains with "passing 'B**' as argument 1 of 'foo(A**)'"

Upcasting through one level of reference is most common, why is this different?


Here's why it doesn't work. Imagine that it did work, and then
consider this:

class A{};
class B: public A{};

void foo(A** a)
{
*a = new A;
}

int main()
{
B* bPtr;
foo(&bPtr);
//Oh no, bPtr now points at an A, but no compiler error!
}

The workaround is to change your main method to:

B* b = new B();
A* a = b;
foo(&a);
//a might be pointing to an A rather than a B...

Tom
Jul 19 '05 #3

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

Similar topics

3
by: Joe | last post by:
I know that I can upcast and /downcast in C#. Can it be done in VB.NET? I haven't seen any documentation on it. TIA, -- Joe VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA...
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
4
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
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
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: 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...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.