answersLogoWhite

0


Best Answer

Static member functions (or static member methods) are local to the class in which they are declared but, unlike non-static member functions, they are not attached to any instance of the class. That is, they have no access to an implicit this pointer.

Static member functions are often used to declare global functions in conjunction with private static member variables. The variables are not global as they are only accessible to the class itself, but because they are static they exist for the lifetime of the application.

The best possible example of a static member function relates to singleton classes. A singleton class is typically a global class for which one instance (and one instance only) must be guaranteed to remain in scope for the entire duration of the program. A typical usage for such a class might be to enable logging (writing a log of all program activity), and we'd only need one logger.

This means we must actively prevent new instances from being created once the first instance is created, and must ensure it cannot be destroyed while the program is active, intentionally or otherwise. In order to achieve this, the default constructor, the copy constructor and the assignment operator must be declared private with no implementation. This then means that only the class itself can instantiate the one and only instance of the class, and the only way to achieve that is with a static member function and a static member variable.

The following demonstrates the skeleton of a singleton declaration. In a real-life singleton, you would design the remainder of the class just as you would an ordinary class, with instance variables and methods that permit it to carry out its role (whether as a logger or as some other global class for which only one instance is ever required). The example includes simple code to demonstrate its usage as a container for an int using dynamic memory.

Note how the static variable Singleton::instance is initialised to NULL outside of the class declaration (at file scope). This ensures correct initialisation at compile time. This line is typically placed in the class cpp file (implementation file), while the class declaration itself is placed in the class hpp file (header). The one and only instance of the Singleton class is created upon the first call to Singleton::Instance(), which returns a reference to the instance thus permitting access to the instance members.

Since the instance is static, it is guaranteed to remain in scope for the entire life-cycle of the program. It doesn't matter that the static Singleton::instance variable is instantiated dynamically but is never actually released. Since it is a static variable, the memory it points to will be released automatically when the program terminates. The same can also be said of the Singleton::instance->pointer member variable, but it's always good practice to release all non-static members in the private destructor.

The design is such that it would be impossible to inadvertently create two or more instances of the Singleton class, and impossible to destroy the one and only instance until the program terminates.

While there are many other examples of static member functions, the majority merely demonstrate usage that could be far better implemented as a non-static member function or even a friend function. Demonstrations of any practical merit are few and far between (although they do exist), but the Singleton usage is by far the most ingenious and practical usage you will encounter. Microsoft may not agree (a Microsoft singleton isn't strictly a singleton), but the framework shown below is tried and tested, is robust, and does exactly what it says on the tin!

#include

using namespace std;

class Singleton{

private:

Singleton():pointer(new int(100)){} // No creation outside the class.

Singleton(Singleton const&){} // No copying outside the class.

Singleton& operator=(Singleton const&){} // No assignment outside the class.

~Singleton(){delete(pointer);} // No destruction outside the class.

static Singleton* instance; // The only instance there can ever be.

int * pointer; // Dynamic memory allocation (for illustrative purpose).

public:

// Static member function:

static Singleton& Instance(){

if( !instance ) // Creates the instance on first usage only.

instance = new Singleton();

VERIFY(instance != NULL); // Not essential, but prudent.

return(*instance);

}

// A member function (for illustrative purposes).

void foo(void){

cout<<"Singleton::instance resides at memory address 0x"<

cout<<"Singleton::instance->pointer resides at memory address 0x"<

}

// A member accessor and mutator (for illustrative purposes).

int GetValue(void){return(*pointer);}

void SetValue(const int value){*pointer=value;}

};

// Static member initialisation (occurs at compile time). Not optional!

Singleton* Singleton::instance = NULL;

int main(void)

{

// Call singleton methods:

Singleton::Instance().foo(); // The one and only instance is instantiated at this point, just prior to calling foo().

Singleton::Instance().SetValue(3); // Mutate the member variable.

Singleton::Instance().foo(); // Call foo() again.

Singleton::Instance().SetValue(15); // Mutate the member variable.

// Call the member variable accessor:

cout<<"Singleton variable: "<

// It is not possible to delete the instance by accident (the destructor is inaccessible outside of the class):

//delete(&Singleton::Instance()); // Causes compiler error C2248! (MSVC++).

return( 0 );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

First, it has to be declared.

class myClass {

...

type myMember(...);

...

}

Second, it has to be defined.

type myClass::myMember(...) {

...

}

You can also declare and define it at the same time.

class myClass {

...

type myMember(...) {

...

}

...

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Instance methods, or object behaviors.

Member, most of time is referred to the one of "instance" (vs static/class member).

Functions are methods. Any method in OOP must be defined and/or implemented by (within) a class, like a member (part) of that class.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A member function of a class defines a method or operation that may be performed upon the class member data, such as returning values from the data, mutating the data, or performing a calculation from the data.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A member function is simply a function (or method in Java) that is part of a class.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

When you declare a class and define a function inside the class, that function is called a member function.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Member functions are the methods associated with a class or structure. They are more commonly referred to as member methods.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

the internal data of a class is called data members.

the internal function of a class is called as member function.member functions are used to manipulate the data.

rama jyotsna

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Method.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does member function of class define?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


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.


Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


Why can't the accessor member function change any of the values in a class in C plus plus?

Nothing stops a member function from changing any of the values in a class. By convention, an accessor function is used to give read only access to class data, but that does not mean that it is prohibited from doing so. It is a member function, after all, and it has all the rights of any member function of the class.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.

Related questions

What is message passing in c plus plus?

-define class with necessary data member &amp; member function. -create object of that class. -communication.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


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.


Can you declare and define the constructor within class?

Yes, you can declare and define the constructor within a class. A constructor is a special member function of a class that is automatically called when an object of the class is created. It is used to initialize the object's data members. The constructor can be declared and defined within the class definition or can be defined outside the class definition using the scope resolution operator (::).


Why can't the accessor member function change any of the values in a class in C plus plus?

Nothing stops a member function from changing any of the values in a class. By convention, an accessor function is used to give read only access to class data, but that does not mean that it is prohibited from doing so. It is a member function, after all, and it has all the rights of any member function of the class.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


How do you design inline function as a member function?

You define the function at the same time you declare it, usually in the header file, sometimes in an .hpp file.


Is object a member of a class?

Yes, you would need to define your variables. Also initialize them


What is the Use of static function?

A static function is a member function that is not associated with any instance of the class; it has no this pointer.


When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


Difference between member function and friend function?

1. Function - is normally refered to a C-style function which has a global level scope. As long as its declaration is visible in a file where it is being used, and the definition is available somewhere in the application, the linker will find the definition and link to it. It can be used anywhere in the application. 2. Member function - is normally referred to a C++Style method declared/defined inside of a C++ class. The scope for such member functions is the class. They are not accessible outside the class and are only accessible thru an object/instance of such a class. There are, of course, exceptions to this, such as static and friends.