Wednesday, December 17, 2008

Three points about c++ class and structure

In last few years , I read lots of c++ books . But I did not do much program work for C++. One reason is lazy that I think I dont need. Second reason is I dont have project which need it.

Now I find it 's useful . But some problems have came.
Today , I learn two points:

1, you can not give value to variable in the class which is forbidden:
class myclass {
int x=5,y;
}

2, You need delete the class(structure)pointer if you dont use it anymore:

myclass *mc * mcc;
mc=new myclass;
mcc=new myclass[2];// a vector

delete mc;
delete [] mcc;

It's same to structure data;


3,for structure, it can not contain itself when you build it
like
structure st{
int x;
int y;
st itself;
} ;

That's forbidden in c++; But as a tip you can make a pointer to itself:

like:
structure st{
int x;
int y;
st* itself;
}

When you want get a chain of data , you need this tip..

No comments: