简体   繁体   English

C ++中未调用构造函数

[英]Constructor is not called in C++

This is the code. 这是代码。

#include<iostream>
using namespace std;

class Item{
   double itemPrice;
   int qty;
   public:
   Item(){
      cout<<"Enter Item Price : "<<endl;
      cin>>itemPrice;
      cout<<"Enter QTY : " <<endl;
      cin>>qty;
   }
   double getItemTotal(){
      return itemPrice*qty;
   }
   };

   class Order{
      int index;
      int orderId;
      double orderValue;
      Item items[20];
  public:
      Order(){
          index=0;
          cout<<"\nEnter Order ID : ";
          cin>>orderId;
  }
  void viewOrderDetails(){
      for(int j=0;j<20;j++){
         Item ii=items[j];
         orderValue=orderValue+ii.getItemTotal();
      }
      cout<<"Order ID   : "<<orderId<<endl;
      cout<<"Order Value   : "<<orderValue<<endl;
  }

  void addToOrder(Item i){
      if(index<19){
         items[index]=i;
         index=index+1;
      }else{
         cout<<"\nOrder Full";
      }
  }
};

int main(){
   Order odr1;

   Item i1;
   Item i2;

   odr1.addToOrder(i1);
   odr1.addToOrder(i2);

   odr1.viewOrderDetails();

   return 0;
}

I want to run the constructor of Order class. 我想运行Order类的构造函数。 But it runs the Constructor of Item class. 但是它运行Item类的构造方法。 I checked the code many times and did a research.But I cant seem any wrong in the code. 我检查了很多遍代码并进行了研究。但是我似乎在代码中没有任何错误。 I am using CodeBlocks IDE with GCC Compiler( MingGW ). 我正在将CodeBlocks IDE与GCC编译器(MingGW)一起使用。 I appreciate if anybody can help me with this. 我很高兴有人可以帮助我。 Thanks. 谢谢。

The constructor of your Order class will get callled. 您的Order类的构造函数将被调用。

Item items[20];  // <-- here you actually create 20 Items and the constructor for each Item will be called. Then the Order Constructor will get called.

You could use std::list<Item> items; 您可以使用std::list<Item> items; instead of Item items[20] . 而不是Item items[20] In that case you don't actually create an Item (and therefore its constructor will not get called) you just create a container where you can store your items. 在这种情况下,您实际上并没有创建Item(因此不会调用其构造函数),而只是创建了一个可以存储项目的容器。

Anyway, it is bad practice to do what you do in your constructor. 无论如何,在构造函数中执行操作是不好的做法。 A constructor should initialize the object and it should run fast. 构造函数应初始化该对象,并且该对象应运行得很快。 So create a method instead. 因此,请创建一个方法。

Your Order class is: 您的订单类别为:

  class Order{
      int index;
      int orderId;
      double orderValue;
      Item items[20];
  public:
      Order(){

       // the body of the constructor

Your Order class contains an array of 20 Item s. 您的Order类包含20个Item的数组。

Before the code in the constructor executes, all class members have to be constructed, first. 在构造函数中的代码执行之前,必须首先构造所有类成员。

Since your Order class contains 20 Item s, each one of them must be constructed first, and Item 's default constructor is going to get called twenty times, before the body of the Order 's constructor starts executing. 由于您的Order类包含20个Item ,因此必须首先构造每个Item ,在Order的构造函数的主体开始执行之前, Item的默认构造函数将被调用20次。 This is how C++ works. 这就是C ++的工作方式。

This is the explanation for why you're seeing the code in Item 's default constructor getting apparently executed before the code in Order 's default constructor. 这就是为什么您看到Item的默认构造函数中的代码明显在Order的默认构造函数中的代码之前执行的原因的解释。

Instead of: 代替:

 Item items[20];

you need to use a vector: 您需要使用向量:

 std::vector<Item> items;

and have addToOrder() use push_back() to initialize the vector. 并让addToOrder()使用push_back()初始化向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM