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

Multiple ineritance question

Hello!

I have s quick question regarding multiple ineritance that I hope that
someone will have the time to look at. I have the following classes that
I try to compile and use:

class A
{
public:
virtual void foo() = 0;
};

class B : public A
{
public:
virtual void bar() = 0;
};

class A_Impl : public A
{
public:
void foo() { }
};

class B_Impl : public A_Impl, public B
{
public:
void bar() { }
};

Now when I try to create an instance of B_Impl I get the following error
message when I try to compile with gcc:

main.cpp: In function `int main()':
main.cpp:7: cannot allocate an object of type `B_Impl'
main.cpp:7: because the following virtual functions are abstract:
A.h:7: virtual void A::foo()

Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
implementation of void foo()?

Regards,
Mattias

Jul 19 '05 #1
2 3829

Mattias B wrote:
...
I have s quick question regarding multiple ineritance that I hope that
someone will have the time to look at. I have the following classes that
I try to compile and use:

class A
{
public:
virtual void foo() = 0;
};

class B : public A
{
public:
virtual void bar() = 0;
};

class A_Impl : public A
{
public:
void foo() { }
};

class B_Impl : public A_Impl, public B
{
public:
void bar() { }
};

Now when I try to create an instance of B_Impl I get the following error
message when I try to compile with gcc:

main.cpp: In function `int main()':
main.cpp:7: cannot allocate an object of type `B_Impl'
main.cpp:7: because the following virtual functions are abstract:
A.h:7: virtual void A::foo()

Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
implementation of void foo()?
...


'B_Impl' has two subobjects of type 'A'. One comes through base class
subobject 'A_Impl', another - through base class subobject 'B'. Virtual
function 'foo' from the former has 'A_Impl::foo' as final overrider,
which is not pure. Virtual function 'foo' from the latter has 'A::foo'
as final overrider, which is _pure_. For this reason, as defined in
10.4/4 class 'B_Impl' is an abstract class and cannot be instantiated.

Provide a non-pure final overrider for all 'foo' functions is your class
'B_Impl' and the code will compile. If I understand your intent
correctly, you need something like this

class B_Impl : public A_Impl, public B
{
public:
void bar() {}
void foo() { A_Impl::foo(); }
};

You should probably also decide whether you really need two distinct
base class subobjects of type 'A' in 'B_Impl' and, maybe, use 'A' as
virtual base class.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #2

"Mattias B" <br****@ludd.luth.se> wrote in message
news:3f*********************@news.luth.se...
Hello!

I have s quick question regarding multiple ineritance that I hope that
someone will have the time to look at. I have the following classes that
I try to compile and use:

class A
{
public:
virtual void foo() = 0;
};

class B : public A
{
public:
virtual void bar() = 0;
};

class A_Impl : public A
{
public:
void foo() { }
};

class B_Impl : public A_Impl, public B
{
public:
void bar() { }
};

Now when I try to create an instance of B_Impl I get the following error
message when I try to compile with gcc:

main.cpp: In function `int main()':
main.cpp:7: cannot allocate an object of type `B_Impl'
main.cpp:7: because the following virtual functions are abstract:
A.h:7: virtual void A::foo()

Can anyone tell me why this is? Surely B_Impl has inerited A_Impl's
implementation of void foo()?


Yes, but not B's. You're inheriting "doubly" from A. Based on the fact
that you're asking this question leads me to think that you probably
actually want virtual inheritance from A.
Jul 19 '05 #3

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

Similar topics

13
by: jing_li | last post by:
Hi, you all, I am a newbee for php and I need your help. One of my coworker and I are both developing a webpage for our project using php. We have a copy of the same files in different location...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
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:
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
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...

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.