473,668 Members | 2,403 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 1362
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******** ******@TK2MSFTN GP12.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 MyImplementatio n();

return instance;
}
}

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

This provides something akin to a singleton, the instantiation of the
MyImplementatio n 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********@nom ailplease.com> wrote in message
news:O0******** ******@TK2MSFTN GP10.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
33173
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 to work before I added the changes in, and I'm not sure if that's causing it.
3
2521
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 << "C"; }}; // virtual or not ? that's the question... int main(int, char**) { A* x; A a; B b; C c;
3
1494
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 compiler dependend, or does the standart say anything about wheter the static or the dynamic call is used? With Visual C++ 5.0,
3
360
by: Radde | last post by:
HI, Can virtual function be static. If so how and why?? Cheers..
5
3000
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
2585
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
1431
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
3495
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
2304
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 = FooFactory::create("./saved/foo2.eb287ac8"); Now, Foo is an abstract base class, and FooFactory contains a static function which again calls static create functions on Foo1 or Foo2 (see code for all classes below). The problem is that all these functions are static, but I also...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
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...
1
8577
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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
5677
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();...
1
2786
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
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.