Results 1 to 4 of 4

Thread: What is static data members of class?

  1. #1
    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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:

    class A
    {
    public:
    static int p; //Static variable
    };
    int A::p = 0; // definition outside the class declaration

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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:

    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()
    {
    }

  4. #4
    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:

    #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;
    }

Similar Threads

  1. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  2. What are the Static storage class in C
    By Garett in forum Software Development
    Replies: 3
    Last Post: 23-01-2010, 11:07 AM
  3. Access the non-public members of the class:C++
    By UseME in forum Software Development
    Replies: 3
    Last Post: 18-01-2010, 11:23 AM
  4. Java example for static members
    By ScarFace 01 in forum Software Development
    Replies: 6
    Last Post: 05-01-2010, 05:14 AM
  5. How to initialize static members in template classes
    By Aienstaien in forum Software Development
    Replies: 3
    Last Post: 05-05-2009, 06:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,316,236.00426 seconds with 17 queries