Click here to Skip to main content
12,631,022 members (31,043 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
why static functions are required?
Posted 17-Dec-12 23:14pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

static = not(dynamic)
not(dynamic) = no instance specific
no instance specific = class specific

therefore, static = shared (run-time sharing of variables/objects between instances of a particular class.)


And for access those static variables/objects We require Static methods/Functions...

static variables/objects are class specific not instance-specific...
so that,
static variables & methods can directly call using classname.


real time example
you want to count how many objects created of particular class
then declare static variable and increment-decrements value when you are create new instance of that class & dispose that instance

Happy Coding!
:)
  Permalink  
Comments
Menon Santosh 20-Dec-12 8:06am
   
+5
Aarti Meswania 20-Dec-12 8:12am
   
Thank you!
:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Static function does not need an instance to call this method. In programming we normally use static method when we need same operation has to be called in different classes, different modules.

eg: Conrol validation has to be performed in diffrent UI class.

inorder write the same logic again and again you can define a static method

class Validator
{
  static bool Validate(TextBox text)
  {
    bool success = false;
 
    if(!string.IsNullOrEmpty(text.Text))
    {
      success = false;
    }
   return success;
  }
}
 
so you can call this method as 
Validator.Validate(myTextBox);
Validator.Validate(mycomboBox); // this is an example
so you dont need to create an instance to call this method. Static methods are very handy.
You can find more generous answer from google or code project. I made it very simple for yours understanding.
  Permalink  
Comments
Menon Santosh 20-Dec-12 8:06am
   
+5
jibesh 20-Dec-12 8:07am
   
Thanks Santosh
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Suppose you declare static variables in your class.
eg

class Example
{
private:
int aVariable;
static int anotherVariable;
};
 
Example::anotherVariable = 0;
Now the property of the static Variable is that it doesn't belong to any particular instance of the class. It is shared by all the instances of the class. It exists even if there are no instances of the class.

Now suppose you would wish to access this variable. You could do that before creating any instances of the class. But how would you access it.

That is where static functions comes into play. With them you could access the static variable.

eg

class Example
{
public:
static int returnStaticVariable()
{
return anotherVariable;
}
private:
int aVariable;
static int anotherVariable;
};

You could call this static function with the Class Name, instead of instance.
eg
int main()
{
std::cout<<Example::returnStaticVariable();
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100