answersLogoWhite

0


Best Answer

A non-static member function has a hidden argument called the this-pointer which points to the data of the specific instance of the class. Static member functions can be called without reference to a specific instance of the class so it is completely what instance if any you mean. You do this by treating them as functions in a namespace with the same name as your class.

Here's an illustrative example:

class CPerson

{ private:

int m_Age; static int m_NumPeople = 0;

public: void SetAge( int Age ){ this->m_Age = Age }; // Correct, if you omit "this->" the compiler will still infer its existance.

CPerson( void ){ m_NumPeople++ };

~CPerson( void ){ m_NumPeople-- };

static int GetAge( void ){ return this->m_Age }; // Wrong, this-> is ambiguous for static functions.

static int GetPeople( void ){ return m_NumPeople}; // Correct, people count is static.

};

There is only one integer m_NumPeople, it behaves like a global variable but the compiler will only let member functions access it since it is private.

m_NumPeople is created with a default value of zero before you even instantiate your first CPerson. As you instantiate CPersons the constructor is called when each instance is created and the instance counter named m_NumPeople is incremented. As these instances are destroyed the destructor decrements m_NumPeople.

Since GetPeople is static you can call it without having access to any instance of the class like so: CPerson::GetPeople();

The compiler has no mechanism to infer which object you called the static function on or even guarantee that the object has ever been instantiated, therefor CPerson::GetAge() is ambiguous and the compiler spits out an error. CPerson::SetAge( 42) is now allowed either. SetAge needs a this-pointer to find the correct instance of m_Age to set.

When you have some instance of the object you use the . operator or -> operator like so: CPerson Nicholas( ); Nicholas.SetAge( 42 ); Now the compiler can uniquely identify the CPerson in question as Nicholas. It will pass the pointer &Nicholas to the SetAge function. Calling conventions vary, but if you're using microsofts visual studio compiler and compiling 32-bit code it will pass the this pointer in the ECX register and the function parameters will be pushed onto the stack in reverse order(but in this case there's only one).

User Avatar

Wiki User

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

Wiki User

14y ago

In C++, a static data member of a class is a member that is shared, or common, between all instances of that class.

A static member function is a function that can only access static data members. It does not have a this parameter, and cannot therefore locate the instance data.

Static data and function members can be used to track class specific things, such as statistics for all instances of a class, or to provide class specific things, such as custom allocators.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

C does not support static data members. You are probably talking about C++ static data members. These are data members that are local to the class in which they are declared, rather than local to objects of the class as per non-static data members.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

static is not a class it is a storage class which is used to declare a local-scope variable.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the features of static data members in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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.


When memory allocate to Static data in c?

Compilation time.


What is classes in programing?

There are no classes in C; it is not an object-oriented programming language. C++ has classes. A class is a data type from which objects can instantiated in much the same way that an integer variable can be instantiated from an int data type in both C and C++. However, an int is a primitive data type; it has no member methods associated with it. The built-in operators are designed to operate upon primitive data types but those operators are not integral to the type. A class is more like a struct in C; an aggregate of data values. A class can contain both static data (data that is common to the class) and non-static data (data that relates to an instance of the class). However, as well as storing data, a class can also define member functions that operate upon that data, but that are scoped to the class (static member functions) or to an instance of the class (instance member functions). Unlike C where a struct's data members are always public, a C++ class can define separate public, protected and private data members (and functions), where private is the default access. A C++ struct is also a class, but one where the members are public by default. As such, a C++ struct can be used to create trivial "plain old data" classes that are compatible with C code as well as to create highly complex data types. Objects are self-contained entities where the member methods (functions and operators) have private access to the class representation. Non-member functions cannot gain access to this representation other than through public member functions or by being declared a friend of the class. The protected representation is the same as the private representation but is also accessible to derivatives of the class. Derivatives automatically inherit the public and protected members of their base classes, but not the private members. This makes it possible to derive more specialised classes from existing classes without have to duplicate the base class code.


What is class in C programming?

There are no classes in C; it is not an object-oriented programming language. C++ has classes. A class is a data type from which objects can instantiated in much the same way that an integer variable can be instantiated from an int data type in both C and C++. However, an int is a primitive data type; it has no member methods associated with it. The built-in operators are designed to operate upon primitive data types but those operators are not integral to the type. A class is more like a struct in C; an aggregate of data values. A class can contain both static data (data that is common to the class) and non-static data (data that relates to an instance of the class). However, as well as storing data, a class can also define member functions that operate upon that data, but that are scoped to the class (static member functions) or to an instance of the class (instance member functions). Unlike C where a struct's data members are always public, a C++ class can define separate public, protected and private data members (and functions), where private is the default access. A C++ struct is also a class, but one where the members are public by default. As such, a C++ struct can be used to create trivial "plain old data" classes that are compatible with C code as well as to create highly complex data types. Objects are self-contained entities where the member methods (functions and operators) have private access to the class representation. Non-member functions cannot gain access to this representation other than through public member functions or by being declared a friend of the class. The protected representation is the same as the private representation but is also accessible to derivatives of the class. Derivatives automatically inherit the public and protected members of their base classes, but not the private members. This makes it possible to derive more specialised classes from existing classes without have to duplicate the base class code.


What variable scope is preferable in modules to avoid overwriting existing data?

C: static other: (default)


What is static language?

A static language is a programming language in which variables must be declared with their specific data types before they can be used. This allows for type checking to be performed at compile-time, catching potential errors before the program is executed. Examples of static languages include C, C++, Java, and C#.


Is there anything similar between C and C plus plus with reference to data hiding?

No. Data hiding is a feature of object oriented programming. C does not support OOP, and therefore has no private member access. All members are public in C.


What are the main features of OOP in c plus plus?

The main features of OOP are the same regardless of the language. They are: encapsulation; data hiding; inheritance; and polymorphism.


What is the use of a Static Constructor?

Static Constructor - It is a special type of constructor, introduced with C#. It gets called before the creation of the first object of a class(probably at the time of loading an assembly). See example below. Example: public class SomeClass() { static SomeClass() { //Static members may be accessed from here //Code for Initialization } }


What is the definition of the term C static?

The term C static is a variable within computer programming in particular C Language. When set static the variable inside a function keeps its value between invocations.


What are non static members in Java?

Local variables (on the stack) or dynamically allocated variables (in the heap) are nonstatic variables. Static variables, constants and globals are all allocated in the program's data segment.


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.