Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Static objects (http://www.go4expert.com/forums/static-objects-t446/)

SunilC 25Sep2005 17:34

Static objects
 
Code:

#include<iostream>
using namespace std;

class A {
       
        int a,b;
        A obj;
       
};
main()
{
        cout<< " making of the object \n";
        A allo;
        cout << "Object completed develpoped\n";
        cout << sizeof(A);
        cout << "now the size of object\n";
        cout << sizeof(allo);
}

When I compiled this code it gives the error I . e. incomplete type
is not allowed.............but when I put static

Such as
Code:

#include<iostream>
using namespace std;

class A {
       
        int a,b;
        static A obj;
       
};
main()
{
        cout<< " making of the object \n";
        A allo;
        cout << "Object completed develpoped\n";
        cout << sizeof(A);
        cout << "now the size of object\n";
        cout << sizeof(allo);
}

It compiles without error giving the output as 8 in cout <<
sizeof(A); and the same 8 also in cout << sizeof(allo);

Can somebody throw some light on the behavior of static object in
its own class.

shabbir 25Sep2005 22:54

Re: Static objects
 
When its non static you are creating a cycle where the object has an object within itself as a member which is practically impossible to have but when it becomes static its equivalent to global object and so compiler does not give any error.

SATYAN JANA 27Sep2005 17:18

Re: Static objects
 
I'll add one thing that,-
just declare the class as:-
class A {
int a,b;
A *obj;
};
Your problem will be solved. Otherwise, it becomes a recursive definition, which is not allowed. You can surely have a self-referencing pointer.


All times are GMT +5.5. The time now is 01:02.