Reputation: 87
I found some statements regarding my above question, but there are diferent claims:
The static member function can't access non-static data members/functions of a class. The vPTR is non-static data member, hence a static member function can't access vPTR.
No, because it doesn't make any sense in C++.
Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.
Which is one is right and for what reason?
Upvotes: 4
Views: 665
Reputation: 179789
The first statement assumes a "vptr". That refers to a common implementation technique in C++ compilers, where each object with virtual functions contains a hidden pointer to some "vtable", which in turn contains pointers to the virtual functions.
This is however an implementation detail of those compilers. They've chosen that implementation because it's a possible implementation, allowed by the C++ standard.
One of the reasons it's allowed is because the C++ standard says that you always need an object to call a virtual function on, so there's always an object to store that "vptr" in.
The first answer therefore confuses cause and effect. Implementations can use a "vptr" because virtual functions need an object and therefore can't be static
, not the other way around.
Upvotes: 0
Reputation: 23792
Which is right one and for what is the exact reason?
The static member function can't access non-static data members/functions of a class. The vPTR is non-static data member, hence a static member function can't access vPTR.
No, because it doesn't make any sense in C++.
Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually.
The first statement is correct, in the sense that static member functions do not have access to the *this
pointer, they have a class scope. A static member is shared by all instances of the class.
In the second statement the first part is opinion-based, one can argue that it could be useful, as @Bathsheba points out. The second part is correct.
Regardless of the possible sense or merit of the use of such construct, the standard is clear as to why virtual static member functions are not allowed:
11.7.2 Virtual functions [class.virtual#11]
[ Note: The virtual specifier implies membership, so a virtual function cannot be a non-member ([dcl.fct.spec]) function. Nor can a virtual function be a static member, since a virtual function call relies on a specific object for determining which function to invoke. A virtual function declared in one class can be declared a friend ([class.friend]) in another class. — end note ]
Upvotes: 2
Reputation: 67723
Can static functions be virtual in C++?
No.
The static member function can't access non-static data members/functions of a class.
That isn't quite right: static methods can absolutely access non-static (instance) members ... if you give them an instance. They have access, they just don't have a default this
object to operate on.
The vPTR is non-static data member, hence a static member function can't access vPTR
We don't need to dwell on implementation details such as vtables. It's sufficient to say that virtual dispatch depends on the dynamic type of an object, and if you don't have an object, there's no type ambiguity that could possibly benefit from virtual dispatch.
C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function virtually
Note that in languages with this facility, you still don't need a special system for static virtuals - it's generally just that classes are objects. So in that case, static methods of a regular class are still instance methods of the class object.
Upvotes: 0
Reputation: 234665
No, static functions cannot be virtual in C++.
It would occasionally be useful if the function does not depend on any members of the class but is, in a sense, dependent on the type:
struct Animal
{
static virtual std::string whatNoiseDoIMake() = 0;
};
struct Dog : Animal
{
static std::string whatNoiseDoIMake()
{
return "woof"s;
}
};
The reason it's not part of the language are due to it not being proposed to and accepted by the C++ standards committee. My example can be solved using type traits, which weakens the case for such constructs to be allowed.
As for const
, that really is about the possibility of modifying non-mutable
class members. It's harder to concoct a meaningful example of a const
static
member function. Perhaps it could apply to static
members, but then such members are reachable via ::
anyway with (interestingly) access specifiers discarded.
Upvotes: 5
Reputation: 1941
No, they can't.
Virtual members were designed to deal with instances of the class.
Static members do not deal with instances, they relate only to class.
How would you select which "virtual static" function to call depending on instance (as in dynamic polymorphism) as there is no connection to any instance?
These are just two different mechanism that have mutually excluding dependencies.
Upvotes: 0
Reputation: 1
Static Functions can not be virtual in c++ since the concept leads to compile error and is basically resolved at run time
Upvotes: -3