I would like to have a base class that does some work and can be
instantiated.
class a
{
public:
virtual int foo() { return 0; };
virtual int bar() { return 0; };
};
I would like to have a derived class that does some slightly different
work and can be instantiated.
class b : public a
{
public:
virtual int foo() { return 1; };
};
I would like to have another derived class that does some slightly
different work in a different area and can be instantiated.
class c : public a
{
public:
virtual int bar() { return 2; };
};
I would like to have another derived class that takes some
functionality from b and some from c:
class d : public b, public c
{
};
I would like the output of the following code
int main()
{
a theA;
printf("%i\n", theA.foo());
printf("%i\n", theA.bar());
b theB;
printf("%i\n", theB.foo());
printf("%i\n", theB.bar());
c theC;
printf("%i\n", theC.foo());
printf("%i\n", theC.bar());
d theD;
printf("%i\n", theD.foo());
printf("%i\n", theD.bar());
return 0;
}
to be:
0
0
1
0
0
2
1
2
Obviously, it isn't since theD doesn't know if it should use
foo()/bar() from the a->b->d inheritance tree or the a->c->d
inheritance tree.
What is the proper way to meet these requirements?
Thanks,
-zip 9 1364
zippy747 wrote: I would like to have a base class that does some work and can be instantiated.
[...] What is the proper way to meet these requirements?
Read about virtual inheritance.
V
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:_7****************@newsread1.dllstx09.us.to.v erio.net... zippy747 wrote: I would like to have a base class that does some work and can be instantiated.
[...] What is the proper way to meet these requirements?
Read about virtual inheritance.
I don't think that will solve his problem, because virtual inheritance relates
to state, and he is trying to define behavior, correct?
jeffc wrote: "Victor Bazarov" <v.********@comAcast.net> wrote in message news:_7****************@newsread1.dllstx09.us.to.v erio.net...
zippy747 wrote:
I would like to have a base class that does some work and can be instantiated.
[...] What is the proper way to meet these requirements?
Read about virtual inheritance.
I don't think that will solve his problem, because virtual inheritance relates to state, and he is trying to define behavior, correct?
Probably. I didn't spend enough time to understand the OP's problem.
Perhaps it can be solved with casting the 'theD' object to 'b' or 'c'
or 'a', explicitly.
V
zippy747 wrote: I would like to have a base class that does some work and can be instantiated.
[SNIP] I would like to have another derived class that takes some functionality from b and some from c:
class d : public b, public c {
};
[SNIP] Obviously, it isn't since theD doesn't know if it should use foo()/bar() from the a->b->d inheritance tree or the a->c->d inheritance tree. What is the proper way to meet these requirements?
Maybe you can try using the "using" directive to specify which foo and
bar you want class d to use.
On Wed, 15 Dec 2004 07:49:39 -0800, zippy747 wrote: I would like to have a base class that does some work and can be instantiated.
<snip> I would like to have another derived class that takes some functionality from b and some from c:
class d : public b, public c {
public:
virtual int foo() { return b::foo(); }
virtual int bar() { return c::bar(); } };
Works?
- Jay
class b : public virtual a
class c : public virtual a
"marieddu78" <ma************@vivacity.it> wrote in message
news:74******************************@localhost.ta lkaboutprogramming.com... class b : public virtual a
class c : public virtual a
This is virtual inheritance. How exactly does this solve the OP's problem?
Virtual inheritance exists to avoid making 2 copies of the base class. This
doesn't have anything to do with the fact that his D object doesn't know which
function to invoke.
Jay Nabonne wrote: On Wed, 15 Dec 2004 07:49:39 -0800, zippy747 wrote:
I would like to have a base class that does some work and can be instantiated.
<snip>
I would like to have another derived class that takes some functionality from b and some from c:
class d : public b, public c {
public: virtual int foo() { return b::foo(); } virtual int bar() { return c::bar(); }
};
Works?
- Jay
What about
class d : public b, public c
{
public:
using b::foo;
using c::bar;
};
?
On Thu, 16 Dec 2004 18:48:49 +0100, Olivier Azeau wrote: Jay Nabonne wrote:
What about
class d : public b, public c { public: using b::foo; using c::bar; };
?
One question is: how should D behave when used in the context of a B (that
is, polymorphically)? That's one thing unclear from the OP. I don't think
the "using" strategy will affect polymorphic behavior. So the correct
approach is determined by the unstated needs... :)
- Jay This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Hunter |
last post by:
I am using pycxx 5.2.2 to implement some extension code and have a
problem relating to inheritance. I have a pure virtual base class and
two concrete derived classes. In the code below, everthing...
|
by: Alex Vinokur |
last post by:
Hi,
I need something like "function inheritance".
typedef void (*func_type1) (int);
typedef int (*func_type2) (double);
typedef char (*func_type3) (short, int);
....
I need a vector...
|
by: apb18 |
last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree
such that the columns 'ID' and 'field' appear in the top level table,
call that table XXX. tables YYY and ZZZ both inherit...
|
by: enchantingdb |
last post by:
I have an exam tomorrow that covers the perceived advantages and
disadvantages of object oriented programming, in particular
polymorphism, inheritance and encapsulation. I know the advantages but...
|
by: jao |
last post by:
My company's product uses Postgres 7.4.3. Postgres is working well for
us, and we've worked through many performance issues by tweaking the
schema, indexes, and posgresql.conf settings.
...
|
by: Terry Hancock |
last post by:
I've been discussing PyProtocols with a a friend
collaborating with me on a SF game project, about
the benefits and design concept of "component architecture",
and I'm a little confused by what...
|
by: Shawnk |
last post by:
Some Sr. colleges and I have had an on going discussion relative to when and
if
C# will ever support 'true' multiple inheritance.
Relevant to this, I wanted to query the C# community (the...
|
by: Dave Rahardja |
last post by:
Since C++ is missing the "interface" concept present in Java, I've been using
the following pattern to simulate its behavior:
class Interface0
{
public:
virtual void fn0() = 0;
};
|
by: Adam Nielsen |
last post by:
Hi everyone,
I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
|
by: Scott David Daniels |
last post by:
Here are some tweaks on both bits of code:
Paul McGuire wrote:
....
m =
for b in bases:
if hasattr(b, '__mro__'):
if MetaHocObject.ho in b.__mro__:
m.append(b)
if m:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
| |