Is it feasible in C++ to possess a member function that's both static and virtual? Apperantly, there is not a straight-forward method of doing it (
static virtual member() is really a complie error), but a minimum of a method to acheive exactly the same effect?
struct Object struct SomeObject : public Object
It can make sence to make use of
GetTypeInformation() both with an instance (
object->GetTypeInformation()) as well as on a category (
SomeObject::GetTypeInformation()), which may be helpful for comparsions and vital for templates.
The only real ways I'm able to think about involves writing two functions / a function along with a constant, per class, or use macros.
Every other solutions?
No, there is no method of doing it, since an amount happen whenever you known as
Object::GetTypeInformation()? It cannot know which derived class version to call since there is no object connected by using it.
You will need to turn it into a non-static virtual function to operate correctly if you should also have the ability to call a particular derived class's version non-virtually with no object instance, you will need to give a second redunduant static non-virtual version too.
Many say it's not possible, I'd go a measure further and say it's not meaningfull.
A static member is one thing that doesn't connect with any instance, simply to the category.
An online member is one thing that doesn't relate straight to any class, simply to a case.
So a static virtual member could be something that doesn't connect with any instance or any class.
No, no chance, because static member functions lack a
this pointer. And static people (both functions and variables) aren't actually class people per-se. They simply are actually invoked by
ClassName::member, and follow the class access specifiers. Their storage is determined somewhere outdoors the category storage isn't produced any time you instantiated an item from the class. Pointers to class people are special in semantics and syntax. A pointer to some static member is really a normal pointer in most regards.
virtual functions inside a class needs the
this pointer, and it is very combined towards the class, so they can not be static.
It's possible. Make two functions: static and virtual
struct Object virtual const TypeInformation &GetTypeInformation() const protected: static const TypeInformation &GetTypeInformationMain1() // Primary function struct SomeObject : public Object virtual const TypeInformation &GetTypeInformation() const protected: static const TypeInformation &GetTypeInformationMain2() // Primary function
I believe what you are attempting to do can be achieved through templates. I am attempting to read between your lines here. What you are attempting to do is call a technique from some code, where it calls a derived version however the caller does not specify which class. Example:
class Foo class Bar : public Foo void Try() int primary()
You would like Try() to call the Bar version of M without indicating Bar. How you do this for statics is by using a template. So change it out like so:
class Foo class Bar template <class T> void Try() int primary()