473,669 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual static member functions?

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.

thanks
Jul 19 '05 #1
2 33174
Hi!

"Vinay Doma" <vi*******@nets cape.net> wrote in message
news:qE******** ************@gi ganews.com...
john smith wrote:
I'm wondering if it's possible to declare a pure virtual member
function?
This is not legal code. virtual functions cannot be declared as static.
That is a correct statement...
From the MSDN - The virtual function mechanism relies on the specific
object that calls the function to determine which virtual function is
used. Since this is not possible for static functions, they cannot be
declared as virtual.


.... but the explanation isn't. Virtual method binding and
invocation are unrelated concepts. One uses the type
of an object, where an instance of the object may or may
not exist; the other uses the instance itself of the object,
which may [for non-static methods] or may not [for
static methods] exist. It is quite possible to see uses
for static virtual method, where an instance of an object
is used to find the version of the static method, but which
method does not use that instance.

If there were a "typeof" operator that would dynamically
resolve the real type of an object at run-time, the following
hypothetical code would work as the "static virtual" might
be implemented:

struct Base
{
static /* "virtual" */ void F();
};

struct Derived : public Base
{
static /* "virtual" */ void F();
}

void G( Base &r )
{
// Note: 'r' is a reference to a polymorphic type, and can
// therefore refer to an instance of the derived class

dynamic_typeof< r>::F(); /* equivalent to r.F(); if static virtuals
existed */
}

Again, that was not really C++ as it currently exists.

Search Google newsgroups section for "static virtual" to
learn more.

Cheers!

- Risto -

P.S. The real reason for the omission of "static virtual" was
originally an oversight, and more recently lack of demand
and/or agreement.

Jul 19 '05 #2
"john smith" <as**@asdf123as df.net> wrote in message news:<bg******* ***@news.eecs.u mich.edu>...
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.


A function cannot be simultaneously virtual and static. The closest
you can come is a virtual non-static function of an object which is
static. For instance, this might do the trick:

class Base {
public:
static Base &instance ();
virtual void f () const = 0;

protected:
Base () { }
~Base () { }

private:
Base (const Base &);
Base &operator= (const Base &);
};

class Derived : public Base {
public:
virtual void f () const { /* ... */ }
};

Base &Base::insta nce () { static Derived x; return x; }

Of course, why you wouldn't just implement the desired behavior inside
Base::f() (without introducing class Derived at all) is a mystery to
me; but it's your code. :)

- Shane
Jul 19 '05 #3

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

Similar topics

5
9381
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. what does a private static member function mean? Thaanx in advance for any advice Rgds,
3
2619
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine one of the member functions. Show that the other is hidden in the derived class. The simple test code I wrote seems NOT to confirm this. The question
11
4597
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
3
2318
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static member functions to be 'const' with the implication that they could not modify static data members?
1
1567
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this function? Rainer
3
3979
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
14
4195
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
5
2278
by: Ralfeus | last post by:
Hi all. I have a base class and several classes inherited from this base class. I need to create a instances counter for each class. I thought about something like creation of virtual static member in the base class and increasing it in each derived class constructor. But it's not possible to create a virtual data member. Which alternative way can be? Or only way is to create a static data member in each class? Thank you
6
3876
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or variables, just static members static void FirstFunction( args & ); static void SecondFunction( args & ); static void ThirdFunction( args & );
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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
8809
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...
0
7407
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...
0
5682
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();...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2797
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
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.