PC Review


Reply
Thread Tools Rate Thread

Avoid inheriting base class to more than one class

 
 
VijayRama
Guest
Posts: n/a
 
      6th Jul 2011
Hi,

How to avoid deriving a class to more than one class?
For example consider I have a Class A ( base class) which is derived
by Class B. Now if I try to derive Class A to Class C it shouldn't be
allowed. How can I do this?

class A
{
}
class B:class A
{
}
class C: class A // deriving class A should not be allowed
{
}

Thanks in advance for your help.

 
Reply With Quote
 
 
 
 
Andrew Baker
Guest
Posts: n/a
 
      6th Jul 2011
You could put something together that prevents you creating instances of
more than one derived class at runtime. I can't think of a way to stop
you writing more than one derived class.

The main question, though, is why on earth have you got this
requirement? Methinks your design is probably (very) wrong..



On 06/07/2011 08:01, VijayRama wrote:
> Hi,
>
> How to avoid deriving a class to more than one class?
> For example consider I have a Class A ( base class) which is derived
> by Class B. Now if I try to derive Class A to Class C it shouldn't be
> allowed. How can I do this?
>
> class A
> {
> }
> class B:class A
> {
> }
> class C: class A // deriving class A should not be allowed
> {
> }
>
> Thanks in advance for your help.
>


 
Reply With Quote
 
RayLopez99
Guest
Posts: n/a
 
      7th Jul 2011
On Jul 6, 10:01*am, VijayRama <vijay3...@gmail.com> wrote:
> Hi,
>
> How to avoid deriving a class to more than one class?
> For example consider I have a Class A ( base class) which is derived
> by Class B. Now if I try to derive Class A to Class C it shouldn't be
> allowed. How can I do this?
>
> class A
> {}
>
> class B:class A
> {}
>
> class C: class A // deriving class A should not be allowed
> {
>
> }
>
> Thanks in advance for your help.


The compiler will stop you if your classes are circular.

As for: class C: class A // deriving class A should not be allowed

Just make a mental note not to do that when you code. If you mean
during run time, then simply don't make C depend on A--presto! you
are done.

RL
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      19th Jul 2011
On 7/7/2011 7:10 AM, RayLopez99 wrote:
> On Jul 6, 10:01 am, VijayRama<vijay3...@gmail.com> wrote:
>> Hi,
>>
>> How to avoid deriving a class to more than one class?
>> For example consider I have a Class A ( base class) which is derived
>> by Class B. Now if I try to derive Class A to Class C it shouldn't be
>> allowed. How can I do this?
>>
>> class A
>> {}
>>
>> class B:class A
>> {}
>>
>> class C: class A // deriving class A should not be allowed
>> {
>>
>> }

>
> The compiler will stop you if your classes are circular.


True, but that is not the case here.

> Just make a mental note not to do that when you code. If you mean
> during run time, then simply don't make C depend on A--presto! you
> are done.


That is a simple solution that may or may not work.

Arne


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      19th Jul 2011
On 7/6/2011 3:01 AM, VijayRama wrote:
> How to avoid deriving a class to more than one class?
> For example consider I have a Class A ( base class) which is derived
> by Class B. Now if I try to derive Class A to Class C it shouldn't be
> allowed. How can I do this?
>
> class A
> {
> }
> class B:class A
> {
> }
> class C: class A // deriving class A should not be allowed
> {
> }


What is is that you want?

Do you want to prevent more than one type of A to be
instantiated within a running program?

Try a run time check a la:

public class A
{
private static Type sub = null;
public A()
{
if(sub == null)
{
sub = this.GetType();
}
else if(sub != this.GetType())
{
throw new Exception("No no my friend");
}
}
}

Do you want to prevent more than one type to inherit from
a public class A in an assembly?

Can not be done. If I get a copy of that assembly on my PC I can inherit
as much as I want.

Do you want to prevent more than one type to inherit from
an internal class A within the same assembly?

You can not make a compile time check. But you can make a runtime
check similar to the above to prevent instantiation of multiple
subclasses.

Do you have a class that you only want to allow inheritance
from in some specific assemblies?

Make it internal and use the InternalsVisibleToAttribute.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting up to inheriting class from base? Brett Romero Microsoft C# .NET 10 7th Jul 2006 09:41 PM
Inheriting Base Class members =?Utf-8?B?c291cmNl?= Microsoft Dot NET 0 18th Apr 2005 10:39 PM
Inheriting a Base class in a web form Peter Cresswell Microsoft C# .NET 3 1st Nov 2004 08:23 AM
Getting type of inheriting class from static methods in base class /kim/birkelund/aka/sekhmet Microsoft Dot NET 4 30th Mar 2004 12:39 PM
Overriding a class base event when inheriting from that class? Brian Mitchell Microsoft VB .NET 3 16th Mar 2004 01:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:37 PM.