+ 1

Non static memeber function with class name

#include <iostream> using namespace std;   class Base {   public:                 void f()   {                   cout << "Base\n";                 } };   class Derived : public Base {   public:                 void f()   {                   cout<<"Derived\n";                 } };   int main() {   Derived obj;   obj.Base::f();   return 0; } Can we call function with class name or like function call f?

4th Jun 2018, 11:39 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
8 Respostas
+ 2
Ipang basically everthing in c++ has a second more verbose name that can be used to clarify stuff for the compiler. the difference between static and non static is that static things have do called as Class::member() and non static stuff has to be called via an object as obj.Class::member() (or shorthand obj.member()) where object is an object of type Class. if you have something like int a; void f(){ int a = 1; ::a=0; cout << a; } which outputs 0 the same principle applies(but the first qualifier is the global scope)
4th Jun 2018, 1:57 PM
Max
Max - avatar
+ 23
You can use a static method if you don't want to instantiate an object. static void f() Base::f(); for non static u have to create instance.. I like object ... advantages of having an object instance means you can create more than one instance of the same class with different data. check out this code 👇 https://code.sololearn.com/cfwqRqhLslvU/?ref=app
4th Jun 2018, 1:21 PM
🌛DT🌜
🌛DT🌜 - avatar
+ 23
Ipang U can just edit my code u will get Derived ob; ob.Super::f();
4th Jun 2018, 2:19 PM
🌛DT🌜
🌛DT🌜 - avatar
+ 5
Max, 🌛DT🌜 can any of you please explain to me this, why the f() was called obj.Base::f() as I see it the f() were not declared with static access modifier, I thought the scope resolution was used for static members, need clarification : ) And also the following pairs seem to output similar result, when do we prefer to choose one over the other? // Base instance obj.Base::f(); // output: Base obj.f(); // output: Base // Derived instance ob.Derived::f();// output: Derived ob.f(); // output: Derived
4th Jun 2018, 1:53 PM
Ipang
+ 5
Max , 🌛DT🌜 Big Thanks guys, both of you, always a new thing to learn a day. Ketan Lalcheta I hope it's okay that I asked them here, I have no intention to hijack the thread, but the question was interesting, so Big Thanks for you to bring it up too : )
4th Jun 2018, 3:29 PM
Ipang
+ 4
Max Big Thanks mate, learned something new today, can I ask you something else please, in case the Base class is extending another class (e.g. Super) is it also possible for a Derived instance to call obj.Super::f() or is it strictly one level up in hierarchy (only obj.Base::f()). Hope I don't bother you much : )
4th Jun 2018, 2:09 PM
Ipang
+ 1
you are accessing the function normaly but the functions of the class you inherit from just have the name Base::f() in your class. the concept you are looking for is called qualified-id
4th Jun 2018, 1:30 PM
Max
Max - avatar
+ 1
Ipang https://code.sololearn.com/cSFY70Dz93Sq/?ref=app but as always what you can access depends on if you inherit public protected or private
4th Jun 2018, 2:28 PM
Max
Max - avatar