Object array initialization without default constructor

后端 未结 11 1931
误落风尘
误落风尘 2020-11-22 04:20
#include 
class Car
{
private:
  Car(){};
  int _no;
public:
  Car(int no)
  {
    _no=no;
  }
  void printNo()
  {
    std::cout<<_no<

        
11条回答
  •  不思量自难忘°
    2020-11-22 04:49

    Nope.

    But lo! If you use std::vector, like you should be (never ever use new[]), then you can specify exactly how elements should be constructed*.

    *Well sort of. You can specify the value of which to make copies of.


    Like this:

    #include 
    #include 
    
    class Car
    {
    private:
        Car(); // if you don't use it, you can just declare it to make it private
        int _no;
    public:
        Car(int no) :
        _no(no)
        {
            // use an initialization list to initialize members,
            // not the constructor body to assign them
        }
    
        void printNo()
        {
            // use whitespace, itmakesthingseasiertoread
            std::cout << _no << std::endl;
        }
    };
    
    int main()
    {
        int userInput = 10;
    
        // first method: userInput copies of Car(5)
        std::vector mycars(userInput, Car(5)); 
    
        // second method:
        std::vector mycars; // empty
        mycars.reserve(userInput); // optional: reserve the memory upfront
    
        for (int i = 0; i < userInput; ++i)
            mycars.push_back(Car(i)); // ith element is a copy of this
    
        // return 0 is implicit on main's with no return statement,
        // useful for snippets and short code samples
    } 
    

    With the additional function:

    void printCarNumbers(Car *cars, int length)
    {
        for(int i = 0; i < length; i++) // whitespace! :)
             std::cout << cars[i].printNo();
    }
    
    int main()
    {
        // ...
    
        printCarNumbers(&mycars[0], mycars.size());
    } 
    

    Note printCarNumbers really should be designed differently, to accept two iterators denoting a range.

提交回复
热议问题