I have 4 building, each building has ten apartments. I created a class for each unit. class unit
to create an object, I need to parameters unit myunit(int building, int apartments)
at the beginning of the program, I need to create an object for each unit and add them to vector, which way is better to do that
the first way, I got Error operand type are: unit = unit *
int main()
{
int building[4] = {1,2,3,4 };
int apts[10] = {1,2,3,4,5,6,7,8,9,10 };
vector<unit > units(40);
int vectorCounter = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
// I got error in the line below.operand type are:unit = unit*
units[vectorCounter++] = new unit (building[i], apts [j]);
}
}
second way: create the same object name but different parameters
int main()
{
int building = {1,2,3,4 };
int apts = {1,2,3,4,5,6,7,8,9,10 };
vector<unit > units(40);
int vectorCounter = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
unit myunit(building[i], apts [j]) ;
units[vectorCounter++] = myunit ;
}
}
third way : same as second way just add the destructor
int main()
{
int building = {1,2,3,4 };
int apts = {1,2,3,4,5,6,7,8,9,10 };
vector<unit > units(40);
int vectorCounter = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
unit myunit(building[i], apts [j]) ;
units[vectorCounter++] = myunit ;
~unit() ;
}
}
which way is better and why I got an error in the first way