1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Static objects

Discussion in 'C++' started by SunilC, Sep 25, 2005.

  1. SunilC

    SunilC New 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.
     
  2. shabbir

    shabbir Administrator Staff Member

    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.
     
  3. SATYAN JANA

    SATYAN JANA New Member

    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.
     

Share This Page