473,503 Members | 12,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple base objects, real-life scenario.

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
{
};
class Derived2: public Base
{
};
class Dervied12:public Derived1, public Derived2
{
//two objects of Base
};
//typical, just no virtual derivation of Base
Just to set the point right, I know we can remove one Base by using
virtual, but that's exactly not the goal here, do we have case where
we need two Base objects, can we compare it with any real life
situation.
Is amphibian comes any way close to this???
your inputs are appreciated.

thanks,
-Paul.
Jul 22 '05 #1
6 2800
Paul wrote:
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
{
};
class Derived2: public Base
{
};
class Dervied12:public Derived1, public Derived2
{
//two objects of Base
};
//typical, just no virtual derivation of Base
Just to set the point right, I know we can remove one Base by using
virtual, but that's exactly not the goal here, do we have case where
we need two Base objects, can we compare it with any real life
situation.
Is amphibian comes any way close to this???
your inputs are appreciated.

thanks,
-Paul.


You are describing the Diamond Inheritance pattern.
This is described in the C++ FAQ. Searching the FAQ
before posting is a very good thing.
http://www.parashift.com/c++-faq-lit....html#faq-25.8

Try this one:
class Field {};
class Record {};

class Look_Up_Field
: public Field,
public Record
{
};

Given a table of records. A record is composed of one or
more fields.

Let there be a Look-up field. The Look-up Field is a _proxy_
for one or more data fields. The Look-up Field can be treated
as a single field in a record or as a record in another table.
Thus it has the behavior of both a single field and as a
record.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2
bg*****@yahoo.com (Paul) wrote in message news:<d8**************************@posting.google. com>...
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
{
};
class Derived2: public Base
{
};
class Dervied12:public Derived1, public Derived2
{
//two objects of Base
};
//typical, just no virtual derivation of Base
Just to set the point right, I know we can remove one Base by using
virtual, but that's exactly not the goal here, do we have case where
we need two Base objects, can we compare it with any real life
situation.
Is amphibian comes any way close to this???
your inputs are appreciated.

thanks,
-Paul.

Yes multiple inheritance is common.
If you are familiar with java you will know about implementing
intefaces.
In C++ Interfaces are called ADT's (abstract data types).
The best example I can think of is the platform specific ATL and COM
where multiple Interfaces are used.
Consider this:
/*Interfaces*/
class Imechanical{
};
class Ibiological{
};
class ILandThing{
};
class ISeaThing{
};

/*classes which use Interfaces*/
class Truck: Imechanical, ILandThing{
};
class AmphibiousTruck: Imechanical,ILandThing, ISeaThing{
};
class Salmon: Ibiological, ISeaThing{
};
class LungFish: Ibiological, ILandThing, ISeaThing{
};

perhaps the Imechanical interface has a needs_oil property and the
Ibiological has a bool property needs_to_reproduce :-)
then these methods are inherited by implementing the appropriate
Inteface.

As I said this is a major concept in Java and also in C# but in C++ it
seems only to be common in specialized areas.

I think it demands a different type of understanding from the basic
C++, a bit like templates, but that's only my perception. I'm sure we
all have different angles of looking at different types of code
solutions. For example:
How do you see this?
class Base{};
class Derived:Base{};
Base* pb = new Derived;
static_cast<Dervied* >(pb);

Do you think of it as casting down or casting up ?

I naturally think of it as casting up as it's being cast to a
superior, or more complicated, object. But I think the general C++
programming community would call it casting down. I'm not 100% sure on
this it's just a book I am reading uses this term in this way. It'd be
appreciated if you could clear this up.

HTH & HTIC
Paul.
Jul 22 '05 #3
Paul wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote in message news:<Jk*****************@newssvr19.news.prodigy.c om>...
Paul wrote:
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
{
};
class Derived2: public Base
{
};
class Dervied12:public Derived1, public Derived2
{
//two objects of Base
};
//typical, just no virtual derivation of Base
Just to set the point right, I know we can remove one Base by using
virtual, but that's exactly not the goal here, do we have case where
we need two Base objects, can we compare it with any real life
situation.
Is amphibian comes any way close to this???
your inputs are appreciated.

thanks,
-Paul.
You are describing the Diamond Inheritance pattern.
This is described in the C++ FAQ. Searching the FAQ
before posting is a very good thing.
http://www.parashift.com/c++-faq-lit....html#faq-25.8


but I am not refering to Diamond Inheritance, I am just looking for a
scenario where we would need two base objects in a derived class. I am
afraid you got it wrong.

Your example _is_ diamond inheritance:
Base
/ \
Derived1 Derived2
\ /
Derived12
Hmm, if smells like a diamond pattern...
Perhaps you should say what you mean and mean what you say.

Try this one:
class Field {};
class Record {};

class Look_Up_Field
: public Field,
public Record
{
};

Given a table of records. A record is composed of one or
^^^^^^^^
more fields.

it should be composition and not inheritance.

If you look very close, I did not specify any inheritance
for class Record. I was giving you a real world example
which matched your query.

Your query was for examples of inheriting from multiple
base classes. That is what I presented.
Perhaps you should say what you mean and mean what you say.
clas Record class Record
^ {
Collection<Field> _fc;
}; Nope, that won't work. In databases, fields can be of more than
one type, such as strings, integers and floating point.

The _implementation_ would be:
class Record
{
Collection<Field *> _fc;
};
With pointers, one can use polymorphism and not care about the
details of each field.
and in your example Look_Up_Field, there are no base copies and is not
an example of Diamond pattern, since there is no common base, its just
half of that diamond. :)

Hmm, look at _your_ original post. Review mine.
I _never_ stated that Look_Up_Field was a diamond pattern.
I _never_ stated that there were base copies, just multiple inheritance.
You never stated anything about copies of base classes.
I stated that your example that you provided was an example of the
Diamond Inheritance pattern.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
Thomas Matthews wrote:
Paul wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote
in message news:<Jk*****************@newssvr19.news.prodigy.c om>...
Paul wrote:

In real life situation, do we ever come across a situation where we
would need two base objects in an object. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The key words here are "would need".
A snippet is worth 1000 words (: so...

class Base
{
};
class Derived1:public Base
{
};
class Derived2: public Base
{
};
class Dervied12:public Derived1, public Derived2
{
//two objects of Base
};
//typical, just no virtual derivation of Base
Just to set the point right, I know we can remove one Base by using
virtual, but that's exactly not the goal here, do we have case where
we need two Base objects, can we compare it with any real life
situation.

While this doesn't look like a question, it is a question. It begins
with "do we have case". So, the question was "Do we ever, in real life,
have a case where we'd need two Base objects in one 'Derved12'?
Hmm, look at _your_ original post. Review mine.
I am puzzled why you cared to reply. You apparently didn't understand
the question.
I _never_ stated that Look_Up_Field was a diamond pattern.
I _never_ stated that there were base copies, just multiple inheritance.
You never stated anything about copies of base classes.
Yes, he did.
I stated that your example that you provided was an example of the
Diamond Inheritance pattern.


Not necessarily. For all we know, 'Base' subobjects of 'Derived1' and
'Derived2' are not the same. You cannot claim diamond inheritance when
there _can_ be a difference between 'Base' subobjects in the same class.

It is not possible in C++, but essentially, the example boils down to

struct Derived : Base, Base {
Derived() : Base(some_argument_1), Base(some_argument_2) {}
};

The only way to simulate that in C++ is by using either containment:

struct Derived {
Base b1, b2;
Derived : b1(some_argument_1), b2(some_argument_2) {}
};

or intermediate derived classes:

struct D1 : Base {
D1() : Base(some_argument_1) {}
};

struct D2 : Base {
D2() : Base(some_argument_2) {}
};

struct Derived : D1, D2 {};

The question of this thread is, "do we ever need that in real life?"

The question _isn't_ "Is that a diamond inheritance?"

The question _isn't_ "How to prevent multiple base class objects?"

Victor
Jul 22 '05 #5
Thomas Matthews wrote:
but I am not refering to Diamond Inheritance, I am just looking for a
scenario where we would need two base objects in a derived class. I am
afraid you got it wrong.

Your example _is_ diamond inheritance:
Base
/ \
Derived1 Derived2
\ /
Derived12
Hmm, if smells like a diamond pattern...
Perhaps you should say what you mean and mean what you say.


What Paul means is:
Have you ever encountered a situation where you *don't* want
a diamond pattern. i.E where

Base Base
| |
Derived1 Derived2
\ /
Derived3

is the correct solution.

(Paul correct me if I got it wrong)

For me the answer is: No. I never have encountered
such a situation.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
Victor Bazarov <v.********@comAcast.net> wrote in message news:<ah**************@newsread1.dllstx09.us.to.ve rio.net>...
Thomas Matthews wrote:
Paul wrote:
Thomas Matthews <Th****************************@sbcglobal.net> wrote
in message news:<Jk*****************@newssvr19.news.prodigy.c om>...

Paul wrote:

> In real life situation, do we ever come across a situation where we
> would need two base objects in an object. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The key words here are "would need".
A snippet is worth 1000 words (: so...
>
> class Base
> {
> };
> class Derived1:public Base
> {
> };
> class Derived2: public Base
> {
> };
> class Dervied12:public Derived1, public Derived2
> {
> //two objects of Base
> };
> //typical, just no virtual derivation of Base
> Just to set the point right, I know we can remove one Base by using
> virtual, but that's exactly not the goal here, do we have case where
> we need two Base objects, can we compare it with any real life
> situation.


While this doesn't look like a question, it is a question. It begins
with "do we have case". So, the question was "Do we ever, in real life,
have a case where we'd need two Base objects in one 'Derved12'?
Hmm, look at _your_ original post. Review mine.


I am puzzled why you cared to reply. You apparently didn't understand
the question.
I _never_ stated that Look_Up_Field was a diamond pattern.
I _never_ stated that there were base copies, just multiple inheritance.
You never stated anything about copies of base classes.


Yes, he did.
I stated that your example that you provided was an example of the
Diamond Inheritance pattern.


Not necessarily. For all we know, 'Base' subobjects of 'Derived1' and
'Derived2' are not the same. You cannot claim diamond inheritance when
there _can_ be a difference between 'Base' subobjects in the same class.

It is not possible in C++, but essentially, the example boils down to

struct Derived : Base, Base {
Derived() : Base(some_argument_1), Base(some_argument_2) {}
};

The only way to simulate that in C++ is by using either containment:

struct Derived {
Base b1, b2;
Derived : b1(some_argument_1), b2(some_argument_2) {}
};

or intermediate derived classes:

struct D1 : Base {
D1() : Base(some_argument_1) {}
};

struct D2 : Base {
D2() : Base(some_argument_2) {}
};

struct Derived : D1, D2 {};

The question of this thread is, "do we ever need that in real life?"

The question _isn't_ "Is that a diamond inheritance?"

The question _isn't_ "How to prevent multiple base class objects?"

Victor


We could also use templates
Example:-

template<class T>
class Base{};

template<class T>
class Derived1:Base<T>{};

template<class T>
class Derived2:Base<T>{};

template<class T1, class T2>
class MoreDerived: Derived1<T1>, Derived2<T2>{};
then instantiate like this:-
MoreDerived<int,double> md;
This combined with derived classes' different implementation of pure
virtual functions is what I was trying to explain in my other
wafflings abour ATL etc.

HTH
Paul.
Jul 22 '05 #7

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

Similar topics

2
5385
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
29
4634
by: MAHESH MANDHARE | last post by:
Hi , Can Anyone explain me exactly why multiple inheritance not used in java and c# thanks, Mahesh -- Have A Good Day, Mahesh, Maheshmandhare@yahoo.co.in
32
14760
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...
47
3591
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
5
3428
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
32
2120
by: moleskyca1 | last post by:
This may be stupid question, but why is sizeof(Base) == 1 in: int main(int argc, char* argv) { class Base { }; cout << sizeof(Base) << endl; return 0; }
6
1820
by: MattWilson.6185 | last post by:
Hi, I'm trying to find out if something is possible, I have a few diffrent lists that I add objects to and I would like to be able to have a wrapper class that won't affect the internal object, for...
0
2642
by: tanish2k | last post by:
hi. I am using c#, visual studio 2003. I need to validate a xml file against schema which itself has 2 more schema imported under it. i have following 2 xsd files : xsd1 --->...
47
3953
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
4
3922
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
0
7193
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
7316
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
7449
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
5562
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,...
1
4992
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
4666
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...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.