answersLogoWhite

0


Best Answer

#include<iostream>

#include<trace.h> // user-defined trace macro

using namespace std;

class X

{

public:

X (int data=0)

: m_data (new int (data))

{

TRACE ("creating X @ 0x%x\n", this);

}

~X ()

{

delete m_data;

TRACE ("destroying X @ 0x%x\n", this);

}

X& operator += (int data)

{

*m_data += data;

return *this;

}

private:

int* m_data;

};

static X x = 0;

void foo()

{

TRACE ("entering foo()\n");

static X x = 0;

x += 1;

TRACE ("exiting foo()\n");

}

int main()

{

TRACE ("entering main()\n");

foo();

TRACE ("exiting main()\n");

}

Output:

1: creating X @ 0x24f698

2: entering main()

3: entering foo()

4: creating X @ 0x24f69c

5: exiting foo()

6: exiting main()

7: destroying X @ 0x24f69c

8: destroying X @ 0x24f698

Explanation of output:

1: The static global variable x is instantiated and initialised with the value 0. Note that instantiation occurs before entering main, even though main is the entry point of the application. The compiler has simply inserted the required code at the entry point, immediately in front of the main function code. Note also that this has nothing to do with x being declared static: if you remove the static keyword x would still be instantiated before main was invoked. Therefore x is implicitly static because it was declared at file scope.

2: The main function is invoked.

3. The foo function is invoked from main.

4. The static function variable foo::x is instantiated and initialised with the value 0.

Note that although foo:x is declared static, it does not physically exist until the function is called for the first time. This is because foo::x is scoped to the function.

5. Exit from the foo function.

Note that although foo::x is no longer in scope it is not destroyed at this point. This is because it was declared static. The function also incremented foo::x so foo:x will still have the value 1 if we call foo again, and will increment its value to 2.

6. Exit from the main function.

Normally the program would exit at this point. However, because we have 2 statics still in memory, the compiler has inserted code in place of the main function's closing brace to destroy the static objects.

7. foo::x is destroyed.

8. Global x is destroyed.

At this point the program can exit.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In c plus plus program using class to represent the static data structure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


Write a program to find the grade obtained by the students of a class using structure?

Write a program to find the grade obtained by the students of a class


What is the difference between static and non static class members in java?

Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();


Will JVM execute static methods by default like static variables in java?

No. Static methods are invoked by using the name of the class instead of the name of an instance of a class. Example: public class Test { public static void methodA { // do something ... } public void methodB { // do something else ... } } A program could use methodA by simply calling it using the class name: // call the static method Test.methodA(); To use methodB(), a program would have to first create an instance of the class then call the method: // call the non-static method Test t = new Test(); t.methodB(); Note that you can call methodA from the instance: // call the static method Test t = new Test(); t.methodA(); However, this is considered bad practice. And you cannot call methodB at all using the class name: // can't do this - compile error Test.methodB();


What is static inner class?

An inner class declared as static is treated as if it were a normal top-level class.

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.


Wt is the different between structure and class?

A structure is a different from a class in the sense that a class can represent data elements as well as their associated functions whereas a structure can represent only data elements,, not their associated functions.


What is static class?

A static class is a class where all the members are declared static.


Why toplevel class cannot be static?

A toplevel class certainly can be static. Static has nothing to do with the level of a class.


What is a static identifier?

A static identifier in a block has execution time lifespan, i.e. its value persists from point of initialization to program termination.A static identifier outside of any block has execution time lifespan, because of its non-block semantics, but the static attribute takes on a different meaning, and means that it is not visible to modules outside of the file it is defined in.(Only applicable to C++ - answer provided for completeness sake) A static identifier in a class is common to all instances of that class. A static method of a class can access only static identifiers of that class.


Write a program to find the grade obtained by the students of a class using structure?

Write a program to find the grade obtained by the students of a class


Are the methods or members of static class static?

A static class may or may not have static members. Adding the static keyword to the class definition won't change this. Note that an inner class which is not static may not contain static members (unless those members are also declared final).


How is memory allocated to the private-data member function when an object is not created during the calling process?

A class can have both static and non-static data. Static data is local to the class while non-static data is local to each object of the class. It makes no difference whether static data is global, local to a file, local to a function or local to a class (whether public, protected or private), all static data is allocated within the program's data segment along with all constants. As such they are allocated at compile time.


How is dynamic initialization of objects is achieved?

by creating class as an static class


What are the storage classes in c?

A storage class defines the visibility and lifetime of variables or/and functions within a C Program. There are following storage classes which can be used in a C Program: auto register static extern


What is the difference between static and non static class members in java?

Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();


Will JVM execute static methods by default like static variables in java?

No. Static methods are invoked by using the name of the class instead of the name of an instance of a class. Example: public class Test { public static void methodA { // do something ... } public void methodB { // do something else ... } } A program could use methodA by simply calling it using the class name: // call the static method Test.methodA(); To use methodB(), a program would have to first create an instance of the class then call the method: // call the non-static method Test t = new Test(); t.methodB(); Note that you can call methodA from the instance: // call the static method Test t = new Test(); t.methodA(); However, this is considered bad practice. And you cannot call methodB at all using the class name: // can't do this - compile error Test.methodB();