answersLogoWhite

0


Best Answer

c: A static function is visible only in the source file containing it. A static member is not defined. c++: A static function is visible only in the source file containing it. A static member variable is common to all instances of a class. A static member function is used to access static members.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
User Avatar

Sahil Saini

Lvl 1
1y ago
Static members Static members can be variables or functions. A static variable holds its value between multiple calls to the function.
More answers
User Avatar

Wiki User

11y ago

Static member functions are used to provide functionality that is closely associated with the class but are not associated with any one instance of the class. Static member functions do not have access to a this instance pointer, and can therefore be called without instantiating any instances of the class. By contrast, non-static member functions are associated with a specific instance of the class and therefore have access to the this instance pointer.

Static member functions can be likened to friend functions (with the same access rights), but are scoped to the class itself. They are often used in conjunction with static member variables.

By way of example, consider the following class:

class MyClass

{

int instance_variable;

static int class_variable;

public:

int GetInstanceVariable() const { return( instance_variable ); }

static int GetClassVariable() const { return( class_variable ); }

};

// initialise static member variable.

int MyClass::class_variable = 0;

Here, every instance of the class shares class_variable and we can access it without even instantiating an instance of MyClass:

int x = MyClass::GetClassVariable(); // x == 0

However, because class_variable is shared amongst all instances of MyClass, any instance of MyClass can alter its value. This can be useful if you need to know how many instances of MyClass have been instantiated, for example, simply by incrementing class_variable in every constructor, and decrementing it in the destructor. Thus the value returned by MyClass::GetClassVariable() tells you exactly how many instances exist at that moment in time.

Static members functions are also useful when dealing with singleton classes. The typical model is as follows:

class MySingleton

{

private:

MySingleton(); // default constructor

MySingleton(const MySingleton&) {}; // copy disabled

MySingleton& operator=(const MySingleton&) {} // assignment disabled

static MySingleton *instance; // the one and only instance of MySingleton

public:

static MySingleton& Instance(){

if(!instance)instance=new MySingleton(); return( *instance );}

static void Destroy(){

if(instance)delete(instance);}

}

// Initialise static member.

MySingleton * MySingleton::instance = 0;

Here, an instance of MySingleton is created the first time you call MySingleton::Instance() and that one instance exists for the entire duration of the program or until you call the MySingleton::Destroy() static member function. This then guarantees that only one instance of MySingleton is ever instantiated at any one time. The class can then be fleshed out with non-static member methods that are only accessible via calls to MySingleton::Instance().

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

A constructor is called during the creation of an instance of that class and it does not have a return type as a member function. It is not a property of the created object and the constructor can not be accessed by the object. It is like a static method that only gets called during object creation.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

When you declare a virtual member function you are implying that your class is intended to act as a base class, and that derivatives of your class can override the virtual function, thus ensuring that no matter which class within the hierarchy you refer to, the most-derived override will always be invoked. In other words, it assures polymorphic behaviour. Non-virtual functions do not act polymorphically. This is why destructors must always be declared virtual in a base class, to ensure derived objects are destroyed in the reverse order of construction, from the most-derived class to the least derived class (construction always begins with the least-derived class). Once a function is declared virtual in a base class, it remains virtual in all its derivatives; the virtual keyword is implied from that point on, but it's good practice to explicitly include the virtual keyword, if only to remind consumers of your derivatives that the function can be overridden.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

difference between static data member and static member function

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between member functions and static member functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


What is the Difference between public static void and static public void?

There is no difference between public static void and static public void


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.

Related questions

How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


What is the Difference between public static void and static public void?

There is no difference between public static void and static public void


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


Why static functions cannot use instant variables?

Static functions are tied to a class, not to a particular object. A static function can only access static variables because it has no knowledge of member variables.


How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How is working of a member function different from a friend function and a non-member function?

With respect to a given class, all functions can be split into four categories: 1. Member functions. 2. Static member functions. 3. Friend functions. 4. Non-member functions. All class member functions have the following three properties with respect to the class in which they are declared a member: 1. Private access to the class representation. 2. Scoped to the class. 3. Invoked through an instance of the class (has a 'this' pointer). Static member functions have the first two properties only. Friend functions have the first property only. Non-member functions have none of these properties.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


What is the difference between complex permittivity and static dielectric conatant?

What is the difference between complex permittivity and static dielectric conatant?


What is the difference between static function and global static function?

'global static'?! There is no such thing.


What are the difference between versions and functions?

Version: static reference to a specific entity of a specific instance. Function: dynamic reference to existing algorithm to perform or execute.


What is the difference between static charge and static electricity?

in my own explanation static charge is a reaction between the (+)protons and (-)electrons an the effect of this reaction is called static electricity.