Konstruktor przy tablicy.

0

Witam! Mam np klase, w ktorej tworzę konstruktor majacy dwa argumenty. Potem w main tworze obiekt typu mojej klasy np:

Pojazd tab[10], to jak wywolac tutaj konstruktor dla pierwszego elementu tablicy?

0

nie da sie. Tablice musisz zrobić dynamiczna i tworzyc za pomoca operatora new.
zrezygnuj z tablic korzystaj z vectora.

3
#include <iostream>
using namespace std;

class Vehicle {
public:
    Vehicle() { cout << "ctor-default\n"; }
    Vehicle(int) { cout << "ctor-args\n"; }
    Vehicle& operator=(const Vehicle&) = delete;
};

int main() {
    Vehicle arr[10] = {Vehicle(10)};
    return 0;
}

http://ideone.com/DA1SP9

Ew.

#include <iostream>
using namespace std;

class Vehicle {
public:
    Vehicle() { cout << "ctor-default\n"; }
    Vehicle(int) { cout << "ctor-args\n"; }
    Vehicle(const Vehicle&) = delete;
    Vehicle& operator=(const Vehicle&) = delete;
};

int main() {
    Vehicle arr[10] = {{10}};
    return 0;
}

http://ideone.com/ZF5G2W

1 użytkowników online, w tym zalogowanych: 0, gości: 1