473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I have 'virtual static function'

I would like to have a parent class that make one of the static function as
abstract (i can't do it in csharp now)
so that all the subclasses have to implement a static function with same
signature...

is there any way to do it?
e.g.
abstract class parent
{
public abstract static CallMe();
}

class child : parent
{
public static CallMe(); // must implement or compile error.
}
thx
Nov 15 '05 #1
5 1347
Hi,

No such thing in C#.
Static methods are, uh, well, static.
Check out this thread for more info:
http://groups.google.com/groups?hl=e...r%3D%26hl%3Den

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"babylon" <am***@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
I would like to have a parent class that make one of the static function as abstract (i can't do it in csharp now)
so that all the subclasses have to implement a static function with same
signature...

is there any way to do it?
e.g.
abstract class parent
{
public abstract static CallMe();
}

class child : parent
{
public static CallMe(); // must implement or compile error.
}
thx

Nov 15 '05 #2
n!
> I would like to have a parent class that make one of the static function
as
abstract (i can't do it in csharp now)
so that all the subclasses have to implement a static function with same
signature...


Static methods do not require an instance in order to be invoked, therefore
you cannot make them virtual as virtual methods require an object instance.

However, whilst you cannot do such a thing. You can achieve similar by using
a static proxy. For example:

public sealed class MyStaticProxy
{
private static IMyInterface instance = null;

private MyStaticProxy()
{
}

public static void SomeFuntion()
{
}
}
Nov 15 '05 #3
n!
I stupidly pressed send by accident, sorry. I'll try and continue (and
pretend I haven't lost any dignity) :)

public sealed class MyStaticProxy
{
private static IMyInterface instance = null;

private MyStaticProxy()
{
}

private IMyInterface Instance
{
get
{
if ( null == instance )
instance = new MyImplementation();

return instance;
}
}

public static void SomeFuntion()
{
Instance.SomeFuntion();
}
}

This provides something akin to a singleton, the instantiation of the
MyImplementation object could easily be a call to a class factory allowing
you to change the actual implementation during initialisation, however the
static methods allow a central access point to the rest of the application.

n!
Nov 15 '05 #4
n!
> private IMyInterface Instance

should of course be:

private static IMyInterface Instance

n!
Nov 15 '05 #5
thx!!!!

"n!" <nf********@nomailplease.com> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
private IMyInterface Instance


should of course be:

private static IMyInterface Instance

n!

Nov 15 '05 #6

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

Similar topics

2
33158
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
3
2505
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
3
1484
by: Sensorflo | last post by:
struct CBase { virtual inline void foo() { puts("base"); } }; void main() { CBase obj; (&obj)->foo(); // static call or } // dynamic (using virtual table) call ? Is it...
3
360
by: Radde | last post by:
HI, Can virtual function be static. If so how and why?? Cheers..
5
2988
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
7
2579
by: sunil | last post by:
Hi, I am dealing with problem where I need virtual+static function: enum {A=0,B=1}; Factory.cpp: ------------------ Base * createInstance(int classType,char *name) { if(classType == A)
17
1413
by: Michael | last post by:
Hi, Could you pleaes let me know when I need to use virtual destctor function in the base class? Thanks in advance, Michael
15
3464
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
2
2298
by: Markus Dehmann | last post by:
I need a simple object serialization, where loading an object from file looks like this: Foo* foo1 = FooFactory::create("./saved/foo1.a321f23d"); Foo* foo2 =...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
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...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
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...
1
5059
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
4724
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
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
0
435
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.