473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple base classes in .NET

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 support multiple inheritance. In
C++ for instance I noticed that the compiler flags an error if you use the
"ref" keyword on a class with multiple base classes. This supports the above
quote. However, under the "CodeClass2.Bas es" property (part the VS
extensibility model), it states that:

"Bases are super types of CodeElements. For Visual Basic and Visual C#
there is always only one element in the collection except when the code type
is a CodeInterface".

This is true of course since these languages only support single (class)
inheritance. However, it should be true for all .NET classes based on the
first quote above. My issue is therefore this. I want to retrieve the base
class of an arbitrary class in an arbitrary code file by invoking
"CodeClass2.Bas es.Item(1)". This works in my testing but will it always work
for all languages in theory, assuming the class I'm targetting is always a
..NET class of course. Thanks in advance.
Jul 13 '07
47 4034
On the other hand, ATL does all its business with template tricks and
doesn't use the virtual function call mechanism very much, if at all.
(I can't recall right now if it uses any virtual functions or not, but
my recollection is that I don't recall any virtual function business
going on when I last studied the ATL Internals book.)

LOL

COM is built on a function dispatch table... in C++ parlance, a v-table.

Now, the implementation might not call those virtual functions
polymorphically , but ATL projects are full of virtual functions.
Jul 19 '07 #21
The basic behaviors that MI allow still exist in .NET. They just require
writing a little more code, while at the same time avoiding some of the
"Just require writing a little more code" = increased maintenance burden.
pitfalls that true MI creates.
No, because all the pitfalls that MI creates are now left to the programmer
to handle manually (i.e. MI member lookup has compiler assist which might
have an ambiguity, in .NET the programmer must explicitly list which method
is being called. And the consumer is still faced with ambiguity, as stated
earlier.)
Jul 19 '07 #22
[Please do not mail me a copy of your followup]

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pamspake the secret code
<uD************ **@TK2MSFTNGP05 .phx.gblthusly:
>MI is only one way to aggregate behaviors, not the only way. In .NET
you can implement multiple interfaces to aggregate multiple behaviors
into a single class. In .NET 2 you also have generics, so you can do
template type tricks:

Sorry, but generics aren't templates, and you can't "do template type
tricks".
>>
class BindableControl <T: T

For instance, that line won't work with generics.
Interesting! OK, I didn't know that.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #23
[Please do not mail me a copy of your followup]

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pamspake the secret code
<#o************ *@TK2MSFTNGP02. phx.gblthusly:
>On the other hand, ATL does all its business with template tricks and
doesn't use the virtual function call mechanism very much, if at all.
(I can't recall right now if it uses any virtual functions or not, but
my recollection is that I don't recall any virtual function business
going on when I last studied the ATL Internals book.)


LOL

COM is built on a function dispatch table... in C++ parlance, a v-table.
COM, yes, but the mechanisms that ATL uses to implement itself are
built around templates and not virtual functions, IIRC.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #24
On Jul 18, 2:12 pm, "Chris Mullins [MVP]" <cmull...@yahoo .comwrote:
"Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote
"Larry Smith" <no_spam@_nospa m.comwrote in message

[Multiple Inheritence]
Moreover, it's rarely even used in the C++ world.

That's just... not right.

The entire STL is full of multiple inheritence. Every C++ programmer's first
program uses it, even if they're not aware of it (IOStream).
It certainly isn't "full of multiple inheritance". You gave one
example, and I don't think there's another. Multiple inheritance is
extremely unintuitive, hard to work with, and 99 times out of 100 it
is indicative of a bad design. 1 time out of 100 maybe you've got
something great with multiple inheritance, but one of the fundamental
philosophies of .NET is to err on the side of safety. Having been a C+
+ programmer for over 10 years, I'm praying multiple inheritance
doesn't ever see the light of day in .NET. Even single inheritance is
abused more often than not, when containment would be a much better
solution.

Jul 19 '07 #25
[Please do not mail me a copy of your followup]

"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
<ul************ **@TK2MSFTNGP05 .phx.gblthusly:
>In all honesty, I've used [multiple inheritance] quite a bit,
and people I've worked with (many,
very hardcore OO purests) have also used it quite a bit. Does this mean
people may have overused it? Possibly. But this happens with every
technology.
So how do you feel about "Design Patterns" saying you should prefer
aggregation over inheritance?
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #26
"Richard" <le************ *@mail.xmission .comwrote:
"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
>>In all honesty, I've used [multiple inheritance] quite a bit,
and people I've worked with (many,
very hardcore OO purests) have also used it quite a bit. Does this mean
people may have overused it? Possibly. But this happens with every
technology.

So how do you feel about "Design Patterns" saying you should prefer
aggregation over inheritance?
I would feel alot better about aggregation if it were better supported by
the language.

If there was an attribute I could use:

[Aggregatable]
public class Point
{
int x; int y;
}

public class Circle
{
[Aggrigates(type =Single)] Point;
int Radius;
}

This code would:
1 - Automatically create an interface (IPoint) (or even an Anonymous Type)
2 - Automatically turn Circle into "Circle : IPoint"
3 - Automatically wire up "Circle.X" & "Circle.Y"

I would love to see a 1:Many aggrigation attribute as well:
public class VectorField
{
[Aggrigates(type =Collection)] Vector;
}

This would automatically:
1 - Turn VectorField into "VectorFiel d : IList<Vector>"
2 - Implement all the various IList<Vectorcod e for me

Even more fun would be:
public class TensorField
{
[Aggrigates(type =Dictionary<Vec tor>)] Tensor;
}

This would automatically:
1 - Turn TensorField into "TensorField:ID ictionary<Vecto r, Tensor>"
2 - Implement all the various IDictionary<Vec tor,Tensorcode for me

As it sits, aggregation (to me) means, "Write a whole lot of wrapper code to
wire up the various methods". In general though, in .Net I over-use
aggrigation simply becuase with single inheritence, there's no choice.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jul 19 '07 #27
[Please do not mail me a copy of your followup]

"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
<u9************ **@TK2MSFTNGP03 .phx.gblthusly:
>"Richard" <le************ *@mail.xmission .comwrote:
>"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
>>>In all honesty, I've used [multiple inheritance] quite a bit,
and people I've worked with (many,
very hardcore OO purests) have also used it quite a bit. Does this mean
people may have overused it? Possibly. But this happens with every
technology .

So how do you feel about "Design Patterns" saying you should prefer
aggregation over inheritance?

I would feel alot better about aggregation if it were better supported by
the language.
I think you missed the point of my question. Design Patterns was
written long before .NET was even an idea at Microsoft. DP isn't
about languages or syntactic sugar. In this case "prefer aggregation
over inheritance" is not saying never to use inheritance, nor is it
saying always use aggregation, but it is saying prefer one over the
other.

You seem to be expressing the exact opposite preference. DP goes
beyond issuing a simple aphorism and goes into the details of why
aggregation should be preferred over inheritance.

Beyond your simple personal preference, what are the reasons for
preferring multiple inheritance over single inheritance?

If even single inheritance is to be less preferred over aggregation,
then the case would seem to be even stronger against multiple
inheritance.
--
"The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
<http://www.xmission.co m/~legalize/book/download/index.html>

Legalize Adulthood! <http://blogs.xmission. com/legalize/>
Jul 19 '07 #28
"Richard" <le************ *@mail.xmission .comwrote:
>
Beyond your simple personal preference, what are the reasons for
preferring multiple inheritance over single inheritance?
My reason is very simple:
Every now & then, I run across problems that are best solved though MI. This
seems to happen with about the same frequency I run across a problem that's
best solved by me creating a new thread. (e.g. every few weeks).

In the MI case, I can work around it. Interfaces & aggrigation allows for
this. It just means I have to write more code - sometimes a good deal more
code.

To me, the evolution of a platform means I write less code, especially for
common use cases. The less code that I write, the less likley that I am to
have bugs in my code.
If even single inheritance is to be less preferred over aggregation,
then the case would seem to be even stronger against multiple
inheritance.
I'm fine (and agree with!) with the argument for MI being "Perfer SI over
MI.".

That's a far cry from "Never use MI!", which is something I don't agree
with.

When I give presentations on concurrency, my logic is:
1 - Prefer Async Methods over anything else
2 - Prefer ThreadPool threads over custom threads.
3 - Prefer Timers & Background workers over custom threads.

.... at the very bottom of the list is, "If you really, really, must, go
ahead and create your own thread.".

This is a far cry from "Remove Custom Threads from the Platform!".

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
Jul 19 '07 #29
Hello,

Let me explain why I miss multiple inheritance.

Consider the following situation. Your class has a property X with a simple
field backing the property, with limited behavior -- all behavior is
independent of other properties. For design-time support you decorate this
property with several attributes, and add ShouldSerialize X and ResetX
methods.

Now to duplicate this property in another class, that has a different base
class (cannot share via inheritance) you must copy the field, all the code,
and the attributes. This is not a big deal with one property, but multiply
it by 20 or so properties and several classes, and you realize it is a
maintenance problem.

Interfaces don't help. An interface only helps when you need to use the
property, allowing you to avoid special casing for each class.

Delegation (aggregation) does not help either, because the code duplication
is at the surface level.

With mixin support in C#, we could create a mixin class for those
properties, and the code duplication is eliminated. If the mixin class does
not share any base classes with the "mixed" classes, there are none of the
usual diamond inheritance problems.

Consider scala's support for mixins as a good model for .net:
http://www.scala-lang.org/intro/mixin.html

Whether or not someone needs mixins depends on their task. Some domain
abstractions can be cleanly broken down into a strict hierarchy. Other
things are less hierarchical by nature and work poorly that way. So I don't
think anyone should claim no one needs mixins, or everyone needs mixins.

Personally I have needed mixins on a recent project and have wound up
pushing lots of members up the hierarchy, into base classes, when I would
prefer not to, just to avoid the pain of delegation.

Let me see if I can explain the motivation behind suggestion, "prefer
aggregation over inheritance". If you only use single inheritance, you are
limited by the type hierarchy. Additionally, multiple inheritance has
problems in some languages.

Those problems do not arise with good mixin support. Mixins can work better
than aggregation for code reuse, and mixin code reuse is not limited by the
type hierarchy. So "prefer aggregation over inheritance" is a reflection of
the languages used (probably c++).

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio graphics editor

"Richard" <le************ *@mail.xmission .comwrote in message
news:Oo******** ******@TK2MSFTN GP03.phx.gbl...
[Please do not mail me a copy of your followup]

"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
<u9************ **@TK2MSFTNGP03 .phx.gblthusly:
>>"Richard" <le************ *@mail.xmission .comwrote:
>>"Chris Mullins [MVP]" <cm******@yahoo .comspake the secret code
In all honesty, I've used [multiple inheritance] quite a bit,
and people I've worked with (many,
very hardcore OO purests) have also used it quite a bit. Does this mean
people may have overused it? Possibly. But this happens with every
technolog y.

So how do you feel about "Design Patterns" saying you should prefer
aggregation over inheritance?

I would feel alot better about aggregation if it were better supported by
the language.

I think you missed the point of my question. Design Patterns was
written long before .NET was even an idea at Microsoft. DP isn't
about languages or syntactic sugar. In this case "prefer aggregation
over inheritance" is not saying never to use inheritance, nor is it
saying always use aggregation, but it is saying prefer one over the
other.

You seem to be expressing the exact opposite preference. DP goes
beyond issuing a simple aphorism and goes into the details of why
aggregation should be preferred over inheritance.

Beyond your simple personal preference, what are the reasons for
preferring multiple inheritance over single inheritance?

If even single inheritance is to be less preferred over aggregation,
then the case would seem to be even stronger against multiple
inheritance.

Jul 20 '07 #30

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

Similar topics

6
6487
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I also tried reinterpret_cast (which is clearly an exceedingly dodgy thing to do; it worked, but I'm not sure whether it should have worked, or whether (the more likely scenario) it was just coincidence?) Finally, after a bit of trawling through the...
6
2840
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 { };
20
10084
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in advance for enlightment ... here's the snippet #!/usr/bin/python
22
23384
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 examples?
5
3454
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 { /* ... */ };
2
1869
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
3
2554
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
13
2502
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo: pass class Bar(object): pass
2
2099
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables from one base class are inherited into two derived classes. You want both derived classes to share member variables of the one base class. You can do this way so you don't need keyword -- friend. You can add virtual public One_Base_Class on...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.