class object problem

Why can't we declare a object of a class inside the class but we can declare a pointer type of that object. See the code below :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 class Base{
    
    int x ;
    
public:
    Base(): x(4) { }
    Base(int _x) : x(_x) { }

    int multiply() {
        return x*5;
    }

    Base* f;    // correct 
    Base  f;    // error !  why??

};


Thanks!
If every bag contains another bag....

Then bag 1 contains bag 2, which contains bag 3, which contains bag 4, which contains bag 5, etc, etc, etc. It goes on forever.

This is what is happening when you put a Base object inside the Base class.



On the other hand... if every bag has a slip of paper that has the address of where to find a bag, that's not a problem.

This is what is happening when you put a Base pointer inside a Base class.
Last edited on
Topic archived. No new replies allowed.