static function
-
Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards
-
Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards
Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)
-
Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards
-
Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards
However you have to take care of static functions and use these only if you are sure that they fit to your design. An alternative way to do this is to just derive the classes that needs your functions from the class they belong to. i.e class A{ public: void funcA(); void funcB(); }; class B: public A { // inherits class A // this class now can use the funcA and funcB as its own }; Its basic OO C++ :-)
-
Hi all, I have two functions of a class and i want to call these two functions inside many functions which are derived from other classes.I don;t want to create an object of the class to which the function belongs everywhere.Is there a way out how i can accomplish this. If making a function static will help: Regards
Maynka wrote:
I don;t want to create an object of the class to which the function belongs everywhere.
You don't need to create objects everywhere, you can accomplish the task with a single instance of the class (the better way to do this is explained by the
singleton
design pattern).Maynka wrote:
If making a function static will help:
Probably this is the most natural solution, because it seems that you don't need to access instance members in your functions (and, indeed, you cannot access such members in static functions). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)
:) nice didn't know that or atleast i forgot about it more likely i missed it in a lecture due to me :zzz: knew that static keeps the varibles and function on the heep (am i correct?) didnt realise it affected how it could be called? is that good programming standard or not? thanks sam
-
Yes you can use static functions for that. You can have like a utility class that you can put helper functions in there. Example usage will be like the following code: //======================== class A{ public: static void myprint(){ printf("static hello ! \n"); } }; class B{ public: void func(){ A::myprint(); // Here you call the static myprint without having to create an object of class A } }; int main() { B *poB = new B(); poB->func(); A::myprint() // Again, you can call myprint with no object creation } //======================== The output of this will be : static hello! static hello! Hope that helps :-)
-
:) nice didn't know that or atleast i forgot about it more likely i missed it in a lecture due to me :zzz: knew that static keeps the varibles and function on the heep (am i correct?) didnt realise it affected how it could be called? is that good programming standard or not? thanks sam
mmm not exactly. If I remember correctly they are allocated in their own memory space. Cannot remember the name though.. have to look it up. Regarding whether or not is a good programming standard or not...well...it depends on how to use them. They should be avoided if another way is possible and good OO techniques should be used instead. Sometimes they are usefull though and their use might be required. Of course I wouldn't use static functions all over the place because static functions have their limitations and so they should be used with care.