473,320 Members | 2,161 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,320 software developers and data experts.

Convert Derived** to Base**

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

void f (A* a[]) {}

int main() {
B* b[2];
b[0] = new B();
b[1] = new B();

f(b);
return 0;
}

Compiler says it cannot convert B** into A**. What am I doing wrong?
Thanks.
Aug 5 '08 #1
5 1484
da*****@mail.ru wrote:
class A {};
class B : public A{};

void f (A* a[]) {}

int main() {
B* b[2];
b[0] = new B();
b[1] = new B();

f(b);
return 0;
}

Compiler says it cannot convert B** into A**. What am I doing wrong?
Nothing. There is no conversion between those. Classes are related.
You are hence allowed to convert between pointers to those classes. But
pointers to those classes are not related, so conversion between
pointers to pointer to those classes does not exist.

What are you trying to accomplish? What does your _real_ 'f' do with
the pointers?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 5 '08 #2
On Aug 5, 5:36*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
dani...@mail.ru wrote:
class A {};
class B : public A{};
void f (A* a[]) {}
int main() {
* B* b[2];
* b[0] = new B();
* b[1] = new B();
* f(b);
* return 0;
}
Compiler says it cannot convert B** into A**. What am I doing wrong?

Nothing. *There is no conversion between those. *Classes are related.
You are hence allowed to convert between pointers to those classes. *But
pointers to those classes are not related, so conversion between
pointers to pointer to those classes does not exist.

What are you trying to accomplish? *What does your _real_ 'f' do with
the pointers?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I want to perform an action on each element of array, polymorphically
(e.g. call virtual function, that was declared in class A). Should I
just use vector<A*instead?
Aug 5 '08 #3
Nevermind, that was a stupid mistake on my part. If I want to use
array polymorphically, I should declare it that way too.

A* b[2];
b[0] = new B();
b[1] = new B();

f(b);
Aug 5 '08 #4
da*****@mail.ru wrote:
class A {};
class B : public A{};

void f (A* a[]) {}

int main() {
B* b[2];
b[0] = new B();
b[1] = new B();

f(b);
return 0;
}

Compiler says it cannot convert B** into A**. What am I doing wrong?
Thanks.
Consider this example (which will fail to compile, but is illustrative).
Ignore the memory leaks, they're not relevant to the point being made.

#include <iostream>

struct B {};

struct D1 : public B
{
void f() const
{
std::cout << "D1::f()" << std::endl;
}
};

struct D2 : public B {};

void f(B *arr[])
{
arr[0] = new D2;
}

int main()
{
D1 *arr[2];
arr[0] = new D1;
arr[1] = new D1;
f(arr);
arr[0]->f(); // BOOM
return 0;
}

If the conversion from D1** to B** were allowed, f() could change arr[0]
to point to a D2 (since a D2 is a B, a D2* can be converted to a B*).
Back in main(), arr[0] is expected to point to a D1. The call
arr[0]->f() would thus be valid. The only problem is...arr[0] no longer
points to a D1! Result: danil52 0, Computer 1.

So it's really just as well that this conversion isn't allowed. The same
thing goes for containers in general. A container of D1s is *not* a
container of Bs, because you can add a D2 to a container of Bs, and you
can't add a D2 to a container of D1s.

Regards,
Stu
Aug 6 '08 #5
On Aug 5, 8:07*pm, Stuart Golodetz
<sgolod...@NdOiSaPlA.pMiPpLeExA.ScEomwrote:
dani...@mail.ru wrote:
class A {};
class B : public A{};
void f (A* a[]) {}
int main() {
* B* b[2];
* b[0] = new B();
* b[1] = new B();
* f(b);
* return 0;
}
Compiler says it cannot convert B** into A**. What am I doing wrong?
Thanks.

Consider this example (which will fail to compile, but is illustrative).
Ignore the memory leaks, they're not relevant to the point being made.

#include <iostream>

struct B {};

struct D1 : public B
{
* * * * void f() const
* * * * {
* * * * * * * * std::cout << "D1::f()" << std::endl;
* * * * }

};

struct D2 : public B {};

void f(B *arr[])
{
* * * * arr[0] = new D2;

}

int main()
{
* * * * D1 *arr[2];
* * * * arr[0] = new D1;
* * * * arr[1] = new D1;
* * * * f(arr);
* * * * arr[0]->f(); // BOOM
* * * * return 0;

}

If the conversion from D1** to B** were allowed, f() could change arr[0]
to point to a D2 (since a D2 is a B, a D2* can be converted to a B*).
Back in main(), arr[0] is expected to point to a D1. The call
arr[0]->f() would thus be valid. The only problem is...arr[0] no longer
points to a D1! Result: danil52 0, Computer 1.

So it's really just as well that this conversion isn't allowed. The same
thing goes for containers in general. A container of D1s is *not* a
container of Bs, because you can add a D2 to a container of Bs, and you
can't add a D2 to a container of D1s.

Regards,
Stu
Thanks Stu. That was a great example!
Aug 6 '08 #6

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

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
2
by: Christian Engström | last post by:
When I compile the below program with Microsoft Visual C++ version 6, I get the results I expect, which is that the program should write out base() derived() base() derived(derived) When I...
7
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
0
by: Mark Parter | last post by:
I'm trying convert an XML Schema (http://www.elframework.org/projects/xcri/efc_r1.0.xsd/view) to a VB.Net class using the XSD tool provided with the .NET 2 SDK. However, it's failing to generate...
24
by: AtariPete | last post by:
Hey All, I have a C# question for you regarding up casting (base to derived). I was wondering about the most elegant way (readable, less code) to cast from a base type to its derived type....
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.