在不调用构造函数的情况下创建Vector元素

时间:2018-03-15 13:19:17

标签: c++ class object vector elements

让我们说我有两班汽车和服务。是否可以为汽车和服务类创建两个矢量对象的元素(注意:我不知道它实际上是否被称为矢量对象)。一旦创建了两个元素,我只想稍后调用car类构造函数来调用服务构造函数来获取用户信息吗?

此外,如果可以,可以在不必将Service构造函数更改为方法的情况下使用吗?

   using namespace std; // I know this is not the best, prof wants us to use it

   class Car { Car() { //Get data from user } };

   class Service { Service(){ //Get data from user } };

   int main () {

       int num;
       vector<Car> car; 
       vector<Service> service;

       car.push_back(Car{});
       service.push_back();

       for (int i = 0; i < car.size(); i++) 
            car[i].display(i);
       cout << endl << car.size() + 1 << ". exit";
       cin >> num;
       service[num].Service::Service();

   }

3 个答案:

答案 0 :(得分:2)

我建议使用std::map代替std::vector,这个选择自然会从您的任务中得出。通过使用它,您将只存储有效的服务元素。

   map<int,Service> service;

   car.push_back(Car{});

   for (int i = 0; i < car.size(); i++) 
        car[i].display(i);
   cout << endl << car.size() + 1 << ". exit";
   cin >> num;
   service[num]; //Service constructor will be called

答案 1 :(得分:1)

我以为你在找这样的东西:

class Car {
private:
    std::string _make;
    std::string _model;
    std::string _vinNumber;

    std::size_t _year;

public:
    Car() : _year( 0 ) {} // default
    Car( const std::string& make, const std::string& model, 
         const std::string& vinNumber, const std::size_t& year ) :
      _make( make ), _model( model ), 
      _vinNumber( vinNumber ), _year( year ) {
    }

    void setCarInfo( const std::string& make, const std::string& model,
                     const std::string& vinNumber, const std::size_t& year ) {
        _make      = make;
        _model     = model;
        _vinNumber = vinNumber;
        _year      = year;
    }

    std::string makeOf() const { return _make; }
    std::string modelOf() const { return _model; }
    std::string vinNumberOf() const { return _vinNumber; }
    std::size_t yearOf() const { return _year; }

};

class Service {
private:
    std::string _dealership;
    std::size_t _currentMiles;

public:
    Serivce() {}

    std::string dealershipOf() const { return _dealership; }
    std:size_t  currentMilesOf() const { return _currentMiles; }

    void setOrChangeDealership( const std::string& dealership ) {
        _dealership = dealership;
    }

    void setOrChangeCurrentMiles( std::size_t miles ) {
        _currentMiles = miles;
    }

    void setOrChangeCurrentMiles( const std::size_t& miles ) {
        _currentMiles = miles;
    }
};

int main() {
    std::vector<Car> cars;
    std::vector<Service> services;

    // you can add Car to vector by either default constructor 
    // to be filled out later or by user defined constructor
    cars.push_back( Car( "Honda", "Civic", "75273250323XMD252AG9", 2017 ) );
    // or
    cars.push_back( Car() );
    // then you can at some point in time update elements in container
    cars[i].setCarInfo( "Dodge", "Charger", "9259356M325D2680A217B", 2015 );

    // As with the service class there is only a "default" constructor
    services.push_back( Service() );
    // Service's members will have to be updated manually and later.

    return 0;
}

无论您使用什么容器,或者即使您有一个类对象的单个实例;将调用该类的CTOR。一个人不会的唯一方法是,如果A:你在类中声明它为protectedprivate,则会阻止声明用于继承和多态的类,或者如果你声明了构造函数作为已删除的函数:SomeClass() = delete。如果没有构造函数由编译器隐式调用或由您明确调用,则没有可能获得类实例。

即使是这样简单的事情:

class A {
public:
    int x;
};

int main() {

   A a;
   a.x = 5;

   return 0;
}

编译器将在A a;上调用A()后面的行a,所以实际上看起来像这样:

 A a();

使用其默认构造函数。

答案 2 :(得分:0)

从C ++ 11开始,您可以对vector和其他容器进行列表初始化。

http://en.cppreference.com/w/cpp/language/list_initialization

这意味着,您可以在初始化时将元素的枚举放在向量中。

您可以在其中使用自己的类构造函数:

std::vector<Car> cars {Car(...), Car(...), Car(...)}; //C++11

由于我无法对您的问题发表评论,这是您的期望吗?