Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member Functions in C++

Tags:

c++

  1. To define a member function, we can use the inline function specifier and either the static or virtual (but not both) specifier.
    Why not both?

  2. A nonstatic member function can have const, volatile, or both function qualifiers.
    How can it have both? If its declared const then how can it be volatile?

like image 923
Mayuresh Avatar asked Jan 28 '26 06:01

Mayuresh


2 Answers

To define a member function, we can use the inline function specifier and either the static or virtual(but not both) specifier. Why not both?

Answer:

virtual keyword indicates run time evaluation, which means which function to call is evaluated on the basis of this at runtime, this is possible because a hidden this pointer gets passed to the member functions implicitly, If a function is marked static then the this pointer is not passed in to the member function. This is because static indicates the function is not associated to an instance but it belongs to the class.

From above virtualism cannot work in the absence of this and static specify's no this. Hence static and virtual cannot be applied at the same time to an function.

A nonstatic member function can have const, volatile, or both function qualifiers. How can it have both? If its declared const then how can it be volatile?

Answer:

A const qualifier indicates that the function will not modify any of the class members. That is the class members are immutable inside the function.However, A const function can still modify class members which are declared as mutable in class definition.

A volatile qualifier indicates to the compiler that do not apply(disable) any optimization's For ex: by caching the values in register and not reading them from memory every time.

Given the above, a function can be const and yet volatile, which tells the compiler that this function does not modify any immutable class members but it can modify mutable class members and that compiler should not apply its own optimization(such as register based caching) while dealing with that particular function.

like image 90
Alok Save Avatar answered Jan 30 '26 20:01

Alok Save


1) static functions are not associated with an instance of the class. virtual functions are a key concept of polymorphism. I.e. calling a child implementation via a pointer or reference to a base class, so they clearly need an instance of the class to be called (unlike static methods). Virtual calls are possible because of the vftable pointer which is present in the object, so you need an object to make these calls.

2) const and volatile are completely different. const means you can't modify immutable class members, volatile means all optimizations are stripped from the processing to prevent skipping checking or modifying mutable members. Take into account that you can have mutable members, which can be both volatile and modified by const functions.

like image 23
Luchian Grigore Avatar answered Jan 30 '26 20:01

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!