pure virtual (as in C++)

  • Thread starter Thread starter Vadim Berezniker
  • Start date Start date
V

Vadim Berezniker

From what I read, the only similiar thing in C# is declaring a method as
abstract. Problem is I wanted to only declare some methods as "pure".
So I obviously cannot use an abstract modifier in this case. Is there
any ways to accomplish this?

If there is no straightforward way, I was thinking of declaring an
abstract class, creating a subclass that implements all the "non-pure"
methods and then having other classes inherit from this new class
instead of the original abstract. Any better ways?
 
An abstract class can have both abstract and non-abstract methods. So
you can add all your non-pure methods in the same abstract base class
itself, instead of adding one more level to the inheritance hierarchy.

Regards
Senthil
<b>i</b>
 
Vadim said:
If there is no straightforward way, I was thinking of declaring an
abstract class, creating a subclass that implements all the "non-pure"
methods and then having other classes inherit from this new class
instead of the original abstract. Any better ways?

Oh doh. What was I thinking. This solution is obviously not gonna work.
 
sadhu said:
An abstract class can have both abstract and non-abstract methods. So
you can add all your non-pure methods in the same abstract base class
itself, instead of adding one more level to the inheritance hierarchy.

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.
 
Vadim said:
sadhu wrote:

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.

Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.
 
Vadim Berezniker said:
Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.

;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}
 
Note that you have to mark the class as abstract if it has any abstract methods or properties

abstract class Foo
{
public abstract void Bar();
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Vadim Berezniker said:
Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.

;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}
 
Whoops! Yeah, thanks.

Richard Blewett said:
Note that you have to mark the class as abstract if it has any abstract
methods or properties

abstract class Foo
{
public abstract void Bar();
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk



;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}
 
Back
Top