Static objects

SunilC's Avatar
Newbie Member
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
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's Avatar
Light Poster
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.