Initialize class with pointer

I don't understand how classes construct when they are instantiated by a pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class cpolygon{
int base;
int height;
int perimeter;
int area;
public:
cpolygon(){
    base=0;
    height=0;
    perimeter=0;
    area=0;
}
cpolygon(const int num):base(num),height(num),perimeter(0),area(num*num){}
};


if I wanted to declare a pointer to an object of this class before an object exists, how does the object get constructed?
1
2
3
4
void main(){
   cpolygon* pSquare;
   return;
}

thanks guys!
You have to construct it somehow.

You can create an object just like normal and use operator& to get a pointer to it that you can assign to the pointer.
1
2
cpolygon square;
pSquare = □


You can also create an object with new but then you have to remember to delete it manually when you no longer need the object.
1
2
3
4
5
pSquare = new cpolygon;

...

delete pSquare;


Topic archived. No new replies allowed.