Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.93 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. c   Object array initialization without default constructor
  2. #include <iostream>
  3. class Car
  4. {
  5. private:
  6.   Car(){};
  7.   int _no;
  8. public:
  9.   Car(int no)
  10.   {
  11.     _no=no;
  12.   }
  13.   void printNo()
  14.   {
  15.     std::cout<<_no<<std::endl;
  16.   }
  17. };
  18. void printCarNumbers(Car *cars, int length)
  19. {
  20.     for(int i = 0; i<length;i++)
  21.          std::cout<<cars[i].printNo();
  22. }
  23.  
  24. int main()
  25. {
  26.   int userInput = 10;
  27.   Car *mycars = new Car[userInput];
  28.   for(int i =0;i < userInput;i++)
  29.          mycars[i]=new Car[i+1];
  30.   printCarNumbers(mycars,userInput);
  31.   return 0;
  32. }
  33.        
  34. cartest.cpp: In function ‘int main()’:
  35. cartest.cpp:5: error: ‘Car::Car()’ is private
  36. cartest.cpp:21: error: within this context
  37.        
  38. #include <iostream>
  39. #include <vector>
  40.  
  41. class Car
  42. {
  43. private:
  44.     Car(); // if you don't use it, you can just declare it to make it private
  45.     int _no;
  46. public:
  47.     Car(int no) :
  48.     _no(no)
  49.     {
  50.         // use an initialization list to initialize members,
  51.         // not the constructor body to assign them
  52.     }
  53.  
  54.     void printNo()
  55.     {
  56.         // use whitespace, itmakesthingseasiertoread
  57.         std::cout << _no << std::endl;
  58.     }
  59. };
  60.  
  61. int main()
  62. {
  63.     int userInput = 10;
  64.  
  65.     // first method: userInput copies of Car(5)
  66.     std::vector<Car> mycars(userInput, Car(5));
  67.  
  68.     // second method:
  69.     std::vector<Car> mycars; // empty
  70.     mycars.reserve(userInput); // optional: reserve the memory upfront
  71.  
  72.     for (int i = 0; i < userInput; ++i)
  73.         mycars.push_back(Car(i)); // ith element is a copy of this
  74.  
  75.     // return 0 is implicit on main's with no return statement,
  76.     // useful for snippets and short code samples
  77. }
  78.        
  79. void printCarNumbers(Car *cars, int length)
  80. {
  81.     for(int i = 0; i < length; i++) // whitespace! :)
  82.          std::cout << cars[i].printNo();
  83. }
  84.  
  85. int main()
  86. {
  87.     // ...
  88.  
  89.     printCarNumbers(&mycars[0], mycars.size());
  90. }
  91.        
  92. class Car {
  93.     int _no;
  94. public:
  95.     Car( int no ) :_no( no ) {
  96.     }
  97. };
  98.  
  99. int main() {
  100.     void* raw_memory = operator new[]( NUM_CARS * sizeof( Car ) );
  101.     Car* ptr = static_cast<Car*>( raw_memory );
  102.     for( int i = 0; i < NUM_CARS; ++i ) {
  103.         new( &ptr[i] )Car( i );
  104.     }
  105.     // destruct in inverse order    
  106.     for( int i = NUM_CARS - 1; i >= 0; --i ) {
  107.         ptr[i].~Car();
  108.     }
  109.     operator delete[]( raw_memory );
  110.     return 0;
  111. }
  112.        
  113. static Car*  Car::CreateCarArray(int dimensions)
  114.        
  115. #define DEFAULT_CAR_INIT 0
  116. Car::Car(int _no=DEFAULT_CAR_INIT);
  117.        
  118. Car *c = (Car*) malloc(sizeof(Car) *userInput);
  119.        
  120. Car** mycars = new Car*[userInput];
  121. for (int i=0; i<userInput; i++){
  122.     mycars[i] = new Car(...);
  123. }
  124.  
  125. ...
  126.  
  127. for (int i=0; i<userInput; i++){
  128.     delete mycars[i];
  129. }
  130. delete [] mycars;
  131.        
  132. static Car* makeArray(int length){
  133.     return new Car[length];
  134. }
  135.        
  136. Car* createCars(unsigned number)
  137. {
  138.     if (number == 0 )
  139.         return 0;
  140.     Car* cars = reinterpret_cast<Car*>(new char[sizeof(Car)* number]);
  141.     for(unsigned carId = 0;
  142.         carId != number;
  143.         ++carId)
  144.     {
  145.         new(cars+carId) Car(carId);
  146.     }
  147.     return cars;
  148. }