在其他类构造函数中初始化一个类对象 [英] Initialize a class object inside the other class constructor

查看:408
本文介绍了在其他类构造函数中初始化一个类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在box.cpp

  class Box 
{
private:
int area;

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

}

在circle.cpp

  #includebox.h
class Circle
{
private:
int area;
盒子;

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

}



现在你可以在Circle类中看到整数值和Box对象。在Circle构造函数中,我很容易将该整数值分配给区域。



一个问题是我给了一个字符串给它指定了Box对象



所以我在Circle构造函数中做的是:

  Circle :: Circle area,string str)
{
this-> area = area;
//这里我将字符串转换为整数值
//让我们说int_str;
//稍后我将int_str赋给Box对象,如下所示:
Box box(int_str);

}

我的意图是访问圆面积值和圆对象面积值。
最后我写的函数const Box& getBoxArea()const;像这样:

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

因此,我得不到正确的值。

我建议写一个计算 int的非成员函数基于输入字符串,然后在 Circle 的构造函数初始化列表中使用。

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


b $ b

然后

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

您只能初始化数据成员。一旦你进入构造函数体,一切都已经为你初始化,你所能做的就是对数据成员进行修改。因此,如果 Box 有一个默认构造函数,您的代码的一个变体将是

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


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:

In box.cpp

  class Box
  {
       private:
       int area;

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

  }

In 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.

One problem is that I am given a string for assigning it to the Box object

So what I did inside the Circle constructor is that:

 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
  {
       return this->box;    
  }

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

解决方案

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.

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

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

这篇关于在其他类构造函数中初始化一个类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆