Initializing Variable Constructors and Objects

Let's say I have the following code:

struct Car{

public:
     Car(){}
     Car(int w, int d){
         wheels = w;
         doors = d;
     }
private:
     int wheels;
     int doors;
};

int main(){
     Car *cars = new Car[10];
     cars[0] = {4, 4};
     cars[1] = Car(4, 4);
}

Given that the structure does not allow setting the value of the wheels and doors other than using the constructor, what would be the best way to assign values ​​to an array of cars? Is there a difference between the last two lines in the above code?

I am currently working on a hash table implementation for school, and the source C ++ code base has a Key-Value pair class with only the setKey method and without the setValue method. Therefore, I basically have to assign values ​​to constructor calls and am not sure if there is a difference between using {} or classname (Key, Value) for assignment.

+4
source share
1 answer

, copy-assignment Car, .

, Car , std::vector ,

std::vector<Car> cars(10, Car(4,4));

, .

0

All Articles