我是C++的新手。那么我有box.cpp和circle.cpp文件。在我解释我的问题,我想给你自己的定义:在其他类构造函数中初始化类对象
在box.cpp
class Box
{
private:
int area;
public:
Box(int area);
int getArea() const;
}
在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;
}
,你可以在Circle类看我有一个整数值和Box对象。在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);
}
我的目的是访问这两个Circle区值和圆形对象面积值。 最后我写了函数const Box & getBoxArea()const;像这样:
const Box& getBoxArea() const
{
return this->box;
}
因此我没有得到正确的值。我在这里错过了什么?
您的代码甚至不应该编译。 'Box'需要一个默认的构造函数。请张贴一些现实的代码。 – juanchopanza
[在另一个类对象中初始化一个对象(在对该构造函数进行一些操作之后)]的可能的重复。(http://stackoverflow.com/questions/19345054/initializing-a-object-in-an-another- class-object-after-doing-some-operations-o) – juanchopanza