简体   繁体   English

在另一个类构造函数中初始化一个类对象

[英]Initialize a class object inside the other class constructor

I am new to C++. 我是C ++的新手。 Well I have box.cpp and circle.cpp files. 好吧,我有box.cpp和circle.cpp文件。 Before I explain my problem I'd like to give you their definitions: 在我解释我的问题之前,我想给你他们的定义:

In box.cpp 在box.cpp中

  class Box
  {
       private:
       int area;

       public:
       Box(int area);
       int getArea() const;

  }

In circle.cpp 在circle.cpp中

   #include "box.h"
   class Circle
   {
      private:
      int area;
      Box box;

      public:
      Circle(int area, string str);
      int getArea() const;
      const Box& getBoxArea() const;  

   }

Now as you can see in the Circle class I have an integer value and Box object. 现在你可以在Circle类中看到我有一个整数值和Box对象。 And in Circle constructor I assign that integer values easily to area. 在Circle构造函数中,我可以轻松地将整数值分配给区域。

One problem is that I am given a string for assigning it to the Box object 一个问题是我被赋予了一个字符串,用于将其分配给Box对象

So what I did inside the Circle constructor is that: 所以我在Circle构造函数中做的是:

 Circle :: Circle(int area, string str)
 {
  this->area = area;
  // here I convert string to an integer value
  // Lets say int_str;
  // And later I assign that int_str to Box object like this:
    Box box(int_str);

 }

My intention is to access both Circle area value and Circle object area value. 我的目的是访问Circle区域值和Circle对象区域值。 And Finally I write the function const Box& getBoxArea() const; 最后我写了函数const Box&getBoxArea()const; Like this: 像这样:

  const Box& getBoxArea() const
  {
       return this->box;    
  }

And as a result I do not get the correct values. 结果我得不到正确的值。 What am I missing here? 我在这里错过了什么?

In constructor of Circle you are trying to create an instance of Box , which is too late because by the time the body of constructor will be executed, the members of Circle shall be constructed already. Circle构造函数中,您正在尝试创建Box的实例,这已经太晚了,因为在构造函数的主体执行时, Circle的成员已经构造。 Class Box either needs a default constructor or you need to initialize box in an initialization list: Class Box要么需要默认构造函数,要么需要在初始化列表中初始化box

Box constructBoxFromStr(const std::string& str) {
    int i;
    ...
    return Box(i);
}

class Circle
{
private:
    int area;
    Box box;

public:
    Circle(int area, string str)
      : area(area), box(constructBoxFromStr(str)) { }
    ...
}

I would suggest writing a non-member function that calculated the int based on the input string, and then use that in Circle 's constructor initialization list. 我建议编写一个非成员函数,根据输入字符串计算int ,然后在Circle的构造函数初始化列表中使用它。

std::string foo(int area) { .... }

then 然后

Circle :: Circle(int area, string str) : box(foo(str)) { .... }

You can only initialize a non-static data member in the initialization list. 您只能初始化初始化列表中的非静态数据成员。 Once you get into the constructor body, everything has been initialized for you and all you can do is perform modifications to the data members. 进入构造函数体后,所有内容都已为您初始化,您所能做的就是对数据成员执行修改。 So one variant of your code which would compile if Box had a default constructor would be 所以如果Box有一个默认的构造函数,你的代码的一个变种就是

Circle :: Circle(int area, string str) : area(area)
{
  // calculate int_str
  ....
  box = Box(int_str);
}

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

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