By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

virtual static member functions?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
Hi!

"Vinay Doma" <vi*******@netscape.net> wrote in message
news:qE********************@giganews.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

P: n/a
"john smith" <as**@asdf123asdf.net> wrote in message news:<bg**********@news.eecs.umich.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::instance () { 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 discussion thread is closed

Replies have been disabled for this discussion.