virtual static member functions?

J

john smith

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
 
R

Risto Lankinen

Hi!

Vinay Doma said:
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.
 
S

Shane Beasley

john smith said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top