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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19355857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:45:08  来源:igfitidea点击:

Initialize a class object inside the other class constructor

c++class

提问by user2878007

I am new to C++. Well I have box.cpp and circle.cpp files. Before I explain my problem I'd like to give you their definitions:

我是 C++ 的新手。好吧,我有 box.cpp 和 circle.cpp 文件。在我解释我的问题之前,我想给你他们的定义:

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. And in Circle constructor I assign that integer values easily to area.

现在,正如您在 Circle 类中看到的,我有一个整数值和 Box 对象。在 Circle 构造函数中,我很容易将整数值分配给 area。

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. And Finally I write the function const Box& getBoxArea() const; Like this:

我的目的是访问圆形区域值和圆形对象区域值。最后我写了函数 const Box& getBoxArea() const; 像这样:

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

And as a result I do not get the correct values. What am I missing here?

结果我没有得到正确的值。我在这里缺少什么?

采纳答案by juanchopanza

I would suggest writing a non-member function that calculated the intbased 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 initializea 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 Boxhad a default constructor would be

您只能在初始化列表中初始化一个非静态数据成员。一旦进入构造函数体,一切都已为您初始化,您所能做的就是对数据成员进行修改。因此,如果Box有默认构造函数,则可以编译的代码的一种变体是

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

回答by LihO

In constructor of Circleyou 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 Circleshall be constructed already. Class Boxeither needs a default constructor or you need to initialize boxin an initialization list:

Circle您的构造函数中,您正在尝试创建 的实例Box,但为时已晚,因为在执行构造函数的主体时,已经构造了 的成员Circle。类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)) { }
    ...
}