Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 20-11-2009
Member
 
Join Date: Jan 2009
Posts: 30
What is static data members of class?

I am beginner in the field of the programming.I don't have sound knowledge about regarding static data members of class.

Are Static Members similar to the normal variable or function? How we can define static data members within class?

If you have any idea regarding static data members, Please share with us.
Reply With Quote
  #2  
Old 20-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: What is static data members of class?

Only one copy of a Static data member of a class exists, and it is shared within all objects of that particular class.

Static data members of the class follow the usual class access rules, except that they can be initialized in scope of the file. Static data members can access other static private as well as protected members of their class.

You can define Static data members of any type except void.

Example of Static member declaration:

Quote:
class A
{
public:
static int p; //Static variable
};
int A::p = 0; // definition outside the class declaration
Reply With Quote
  #3  
Old 20-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: What is static data members of class?

Classes can contain static member data and Static member functions. When you declare a data member as static, only one copy of the data is maintained for all objects of the class.

Static data members are not the part of the objects of a given class type. As a result of this , the declaration of a static data member is not considered as a definition. Static data members have external linkage. The below code shows this:

Quote:
class BufferOutput1
{
public:
// Return number of bytes written by any object of this class.

short BytesWritten()
{
return bytecount1;
}


static void ResetCount1()
{
bytecount1 = 0;
}

// Static member declaration.
static long bytecount;
};

// Define bytecount in file scope.
long BufferOutput1::bytecount;

int main()
{
}
Reply With Quote
  #4  
Old 20-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: What is static data members of class?

Each class template instantiation has its own copy of static data members. Static declaration can be of template argument type or of any the defined type.Please go through following example of the static data members:

Quote:
#include <iostream>

class MyDemo
{
public:
MyDemo(int age = 1):intValue(age){
InstanceCount++;
}
virtual ~MyDemo() {
InstanceCount--;
}
virtual int getValue() {
return intValue;
}
virtual void setValue(int age) {
intValue = age;
}
static int InstanceCount;

private:
int intValue;

};

int MyDemo::InstanceCount = 0;

int main()
{
const int count = 5;
MyDemo *MyClassHouse[count];
int i;
for (i = 0; i<count; i++)
MyClassHouse[i] = new MyDemo(i);

for (i = 0; i<count; i++)
{
std::cout << "There are ";
std::cout << MyClass::InstanceCount;
std::cout << " left!\n";
std::cout << "Deleting the one which is ";
std::cout << MyClassHouse[i]->getValue();
delete MyClassHouse[i];
MyClassHouse[i] = 0;
}
return 0;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is static data members of class?"
Thread Thread Starter Forum Replies Last Post
Static method in Abstract class Anthony12 Software Development 6 12-08-2010 10:22 AM
What are the Static storage class in C Garett Software Development 3 23-01-2010 11:07 AM
Access the non-public members of the class:C++ UseME Software Development 3 18-01-2010 11:23 AM
Java example for static members ScarFace 01 Software Development 6 05-01-2010 05:14 AM
How to initialize static members in template classes Aienstaien Software Development 3 05-05-2009 06:36 PM


All times are GMT +5.5. The time now is 03:49 AM.