471,325 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Generic argument as a base class

Howdy all.

Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter
This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.

H^2
Jan 20 '06 #1
3 7206
Harold Howe <hh***@gowebway.com> wrote in news:O$7uGXeHGHA.3936
@TK2MSFTNGP12.phx.gbl:
This feature would be most useful, particularly in the design of a class
library. It would allow users to inject different behaviors into the
leaf classes in the hierarchy.


I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything... What/how would you actually use the Test<T>
class? It would be exactly the same as the T class itself. You wouldn't
be able to override any methods of T because the programmer (not to mention
the compiler) doesn't know what the methods of T are.

Maybe you are searching for 'interfaces'?

-mdb
Jan 20 '06 #2
I'm not quite sure what you are trying to get at, but it seems that this
wouldn't buy you anything...


This is a common technique for implementing the strategy pattern,
especially in other languages. See Modern C++ by Alexandrescu.

Here is a contrived example that uses graphical visio like objects.

class BaseNode
{
public Rectangle Bounds;
public Point Location;
public Size Size;
public virtual void Paint(Graphics g){}
}

class SolidNode
{
public Color BackColor;
public override void Paint(Graphics g)
{
using(SolidBrush brush = new SolidBrush(BackColor)
g.FillRectangle(brush, bounds);
}
}

class GradientNode
{
public Color StartColor;
public Color EndColor;
public float Angle;

public override void Paint(Graphics g)
{
using(LinearGradientBrush brush ... )
g.FillRectangle(brush, bounds);
}
}

class EllipticalNode<TBackgroundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// set eliptical clip region
// paint background
base.Paint(g);

// paint elliptical border
g.DrawElipse(...)
}
}
class BoxNode<TBackgroundType> : TBackgroundType
where TBackgroundType : BaseNode
{
public override void Paint(Graphics g)
{
// paint background
base.Paint(g);

// paint rectangular border
g.DrawRectangle(...)
}
}

The purpose of the templates is to alter the behavior of the leaf
classes via base class selection. Here, the base class dictates how the
background of the node is painted. Without generics, the two leaf
classes would balloon to four classes (GradientBox, SolidBox,
GradientElipse, SolidElipse), or the classes would need to use
aggregation (ie you would pass them an object that is responsible for
painting the background).

H^2
Jan 20 '06 #3
Harold,
Is it possible to use a generic argument as a base class? Based on the
error that I am getting, I think the answer is no.

public class Test<T> : T
{

}

main.cs(5,24): error CS0689: Cannot derive from 'T' because it is a type
parameter


The reason this is not allowed is that the type of T is not known at
compile-time. Therefore, the compiler does not know what the superclass of
Test<T> is.

I know it seems that if the compiler can figure out <T>, the compiler should
be able to figure out : T. However, these are two are different things in
reality. <T> represents the generic type that the compiler expects
developers to define when creating instances of the Test<T> class, when
extending the class, etc. However, : T defines the superclass of Test<T>.
Since T is unknown at compile time, Test<T> can not inherit from T.

I hope that helps.

Regards,

Randy
Jan 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Arthur Dent | last post: by
8 posts views Thread by kasper.rung | last post: by
3 posts views Thread by =?Utf-8?B?Sm9hcXVpbg==?= | last post: by
3 posts views Thread by webcliff | last post: by

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.