Strange Initialize..........
-
In "Programming Priciples and Practice Using C++". chapter 17.5 destructors
//a very simplified vector of doubles
class vector{
int sz; // the size
double* elem; //a pointer to the elements
public:
vector(int s) //constructor
:sz(s), //"initialize sz", I have no idea what does it mean that writting?
elem(new double[s]) //initialize elem, and the same as above
{
for(int i=0; i < s;i++) elem[i]=0; //initialize elements
}
int size() const {return sz;} //the current size
}; -
In "Programming Priciples and Practice Using C++". chapter 17.5 destructors
//a very simplified vector of doubles
class vector{
int sz; // the size
double* elem; //a pointer to the elements
public:
vector(int s) //constructor
:sz(s), //"initialize sz", I have no idea what does it mean that writting?
elem(new double[s]) //initialize elem, and the same as above
{
for(int i=0; i < s;i++) elem[i]=0; //initialize elements
}
int size() const {return sz;} //the current size
};//"initialize sz", I have no idea what does it mean that writting? it means "call sz's constructor with the value in s." it's just another way to initialize member variables.
-
In "Programming Priciples and Practice Using C++". chapter 17.5 destructors
//a very simplified vector of doubles
class vector{
int sz; // the size
double* elem; //a pointer to the elements
public:
vector(int s) //constructor
:sz(s), //"initialize sz", I have no idea what does it mean that writting?
elem(new double[s]) //initialize elem, and the same as above
{
for(int i=0; i < s;i++) elem[i]=0; //initialize elements
}
int size() const {return sz;} //the current size
};forPower wrote:
vector(int s) //constructor :sz(s), //"initialize sz", I have no idea what does it mean that writting? elem(new double\[s\]) //initialize elem, and the same as above
It means set the variable
sz
to the value of the constructor parameters
, and set the variableelem
to point to a new array of doubles, of lengths
. It is a shorthand alternative to writing the linessz = s;
elem = new double[s]; -
In "Programming Priciples and Practice Using C++". chapter 17.5 destructors
//a very simplified vector of doubles
class vector{
int sz; // the size
double* elem; //a pointer to the elements
public:
vector(int s) //constructor
:sz(s), //"initialize sz", I have no idea what does it mean that writting?
elem(new double[s]) //initialize elem, and the same as above
{
for(int i=0; i < s;i++) elem[i]=0; //initialize elements
}
int size() const {return sz;} //the current size
};A value can be put in a variable by either initialization and assignment. Here is the difference -
// Initializing a variable with a value.
double d = 25.4;// Assigning a value to a variable.
double d;
d = 25.4;What you're doing is initializing a class member variable because you cannot do the initialization as I did above inside a class. (You can if the variable is a
static const
variable.) Read about it here - Initializing C++ Class Members[^]«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)