How are we able to call a base class static method by using a derived class name? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How are we able to call a base class static method by using a derived class name?

I understand the static methods are in class scope in c++, so if we have a derived class and we have defined a static function in base class, shoudn't they be in different class scope? And if yes, how are we able to call base class static method using derived class name, such as Derived::baseClassStaticMethod()?

15th Mar 2018, 3:37 AM
Ashish Gaur
Ashish Gaur - avatar
12 Answers
+ 3
Same scope? Why you think static functions are of class scope. They are shared across your codes within a same namespace. (i.e if you modify static in one class, it will change also in another class which call it) So, they have wide scope. What makes it accessible from outside is due to 'public' keyword. Even if you don't inheritent from Test class, you can access static method of some other classes if it is public. PS: No, not same scope. You inheritent(copy) attributes from parent. It's up to you whether you modify them or keep them.
15th Mar 2018, 6:15 AM
Sylar
+ 6
@Ashish Gaur, I guess @Sylar's recent answer explained it pretty well, I was wondering if I misunderstood your point, I'm glad you found your solution. Thanks guys : )
15th Mar 2018, 7:17 AM
Ipang
+ 5
@Sylar I'm thinking *probably* what @Ashish Gaur meant by scope here was not regarding public or private, maybe he was referring to scope of static as class methods, and non static as instance methods, and whether or not Test::fun() can be called from an instance of Derived class, again *probably*...
15th Mar 2018, 6:32 AM
Ipang
+ 5
Good explanation @Sylar #thumbsup : )
15th Mar 2018, 7:08 AM
Ipang
+ 3
Accessible or not depend on access modifier, not on static. Access modifier mean public, private, or protected. Static mean shared across codes (Within a same namespace ofcourse!). But if private, you cannot access. If public, you can access. And further more, if public static, you can directly access. If public not static, you need to instantiate your base class first in order to use it. I may be wrong because i haven't try c++ before. I answer with my assumption in OOP. So, other c++ programmers will know more than me.
15th Mar 2018, 5:01 AM
Sylar
+ 2
I assume you're trying to get some kind of polymorphic behavior with this, but static functions are not the way to achieve that. What exactly are you attempting to do by using the derived class name to call the base class static function?
15th Mar 2018, 4:14 AM
Zeke Williams
Zeke Williams - avatar
+ 2
@Zeke, thanks for responding but I'm not looking for a polymorphic behaviour here. I wanted to know how the static function of a base class, which can be called as Base::staticFunction() can also be called by using any derived class name instead of base class name. How does compiler achieve that? Here is the code if I'm not clear: class Test { public: static void fun() { cout << "Static" << endl; } }; class Derived: public Test { }; int main() { Test::fun(); Derived::fun(); return 0; }
15th Mar 2018, 5:37 AM
Ashish Gaur
Ashish Gaur - avatar
+ 2
As i said above, your code will do exactly same. Here's how: Test::fun() --> output: Static You access static method with its own class name. No magic here, right? Derived::fun() --> output: Static First, Derived class inherited all attributes (which is fun method) from Test class (base class) Second, You call fun() via Derived class. It is allowed because you... well... inherited from Test class :D. And you know what, fun() is also public. You can access it! yay! :D And one more, it is also static. So, you don't need to instantiate Test class to use fun() method! It's clear now, right? Hope i help a little. :) Edit: How compiler achieve? Compiler saw you are using inheritence mechanism in order to call fun(). So, it's happy to compile. ;)
15th Mar 2018, 5:57 AM
Sylar
+ 2
I think you can call it via instance. Ok let me give an example: First, you instantiate derived class: Derived* abc = new Derived() Second, you call fun() like this: abc->fun() abc object first find attribute name 'fun'. It's not there. So, it look up in her parent class (derived class) which she instantiated from. It's there? No. There's no attribute with name 'fun' in derived class too. So it further loop up in her grand-parent class (Test class). And there you go, she found attribute named 'fun' and it's accessible (public). This is what i know about mechanism if object don't have specified attribute. That is! Sorry if it is unclear. :)
15th Mar 2018, 7:03 AM
Sylar
+ 2
Yes iPang, that's exactly what I meant by class scope
15th Mar 2018, 7:11 AM
Ashish Gaur
Ashish Gaur - avatar
+ 1
@Syler, your understanding is correct about static and all those you explained. It's the same in c++ as well. But i wanted to know something else as in my previous comment. Thanks for your reply.
15th Mar 2018, 5:40 AM
Ashish Gaur
Ashish Gaur - avatar
+ 1
Thanks Sylar, that was pretty much I had in my mind but what confused me was that static function are of class scope, so will the base class scope and drives class scope be same?
15th Mar 2018, 6:02 AM
Ashish Gaur
Ashish Gaur - avatar