使用另一个类作为返回类型的类?

Posted

技术标签:

【中文标题】使用另一个类作为返回类型的类?【英文标题】:Classes that use another class as a return type? 【发布时间】:2013-12-24 15:43:36 【问题描述】:

我一直在阅读我关于 C++ 的书,到目前为止我的表现还不错,但在最后一节中,我读到的内容完全超出了我的想象。

我了解返回类型,但是方法如何使用另一个类作为返回类型?当您这样做时,您是从 Point 类创建数据变量和方法的实例吗?我完全迷失了这行:(不是const,或函数定义,只是上面所说的概念)

Point GetUpperLeft() const  return itsUpperLeft; 

但是,这条线很有意义

Point itsUpperLeft;

因为,该类正在创建具有所有 x 和 y 坐标的点的类实例。 GetUpperLeft() 是如何使用函数工作的,它也是一个具有 x 和 y 坐标的对象?

这是其余的代码。

// Begin Rectangle.h
2: #include <iostream>
3: class Point // holds x,y coordinates
4: 
5: // no constructor, use default
6: public:
7: void SetX(int x)  itsX = x; 
8: void SetY(int y)  itsY = y; 
9: int GetX()const  return itsX;
10: int GetY()const  return itsY;
11: private:
12: int itsX;
13: int itsY;
14: ; // end of Point class declaration
15:
16:
17: class Rectangle
18: 
19: public:
20: Rectangle (int top, int left, int bottom, int right);
21: ~Rectangle () 
22:
23: int GetTop() const  return itsTop; 
24: int GetLeft() const  return itsLeft; 
25: int GetBottom() const  return itsBottom; 
26: int GetRight() const  return itsRight; 
27:
28: Point GetUpperLeft() const  return itsUpperLeft; 
29: Point GetLowerLeft() const  return itsLowerLeft; 
30: Point GetUpperRight() const  return itsUpperRight; 
31: Point GetLowerRight() const  return itsLowerRight; 
32:
33: void SetUpperLeft(Point Location) itsUpperLeft = Location;
34: void SetLowerLeft(Point Location) itsLowerLeft = Location;
35: void SetUpperRight(Point Location) itsUpperRight = Location;
36: void SetLowerRight(Point Location) itsLowerRight = Location;
37:
38: void SetTop(int top)  itsTop = top; 
39: void SetLeft (int left)  itsLeft = left; 
40: void SetBottom (int bottom)  itsBottom = bottom; 
41: void SetRight (int right)  itsRight = right; 
42:
43: int GetArea() const;
44:
45: private:
46: Point itsUpperLeft;
47: Point itsUpperRight;
48: Point itsLowerLeft;
49: Point itsLowerRight;
50: int itsTop;
51: int itsLeft;
52: int itsBottom;
53: int itsRight;
54: ;
55: // end Rectangle.h

1: // Begin Rect.cpp
2: #include “Rectangle.h”
3: Rectangle::Rectangle(int top, int left, int bottom, int right)
4: 
5: itsTop = top;
6: itsLeft = left;
7: itsBottom = bottom;
8: itsRight = right;
9:
10: itsUpperLeft.SetX(left);
11: itsUpperLeft.SetY(top);
12:
13: itsUpperRight.SetX(right);
14: itsUpperRight.SetY(top);
15:
16: itsLowerLeft.SetX(left);
17: itsLowerLeft.SetY(bottom);
18:
19: itsLowerRight.SetX(right);
20: itsLowerRight.SetY(bottom);
21: 
22:
23:
24: // compute area of the rectangle by finding sides,
25: // establish width and height and then multiply
26: int Rectangle::GetArea() const
27: 
28: int Width = itsRight-itsLeft;
29: int Height = itsTop - itsBottom;
30: return (Width * Height);
31: 
32:
33: int main()
34: 
35: //initialize a local Rectangle variable
36: Rectangle MyRectangle (100, 20, 50, 80 );
37:
38: int Area = MyRectangle.GetArea();
39:
40: std::cout << “Area: “ << Area << “\n”;
41: std::cout << “Upper Left X Coordinate: “;
42: std::cout << MyRectangle.GetUpperLeft().GetX();
43: return 0;
44: 

【问题讨论】:

你来自 Java 吗? 另外,矩形可能不应该为每个角存储一个点,它是多余的,可能会给你带来麻烦。另外,成员不应该是“itsXXX”,而应该是“myXXX”。 它是 C++ 并且“它与我的”崩溃完全基于意见。 【参考方案1】:

itsUpperLeft 是类Point 的一个实例。

GetUpperLeft() 以其当前形式复制itsUpperLeft 并将该副本返回给调用者。

然后调用者可以调用该副本上的方法,例如GetX()

【讨论】:

【参考方案2】:

GetUpperLeft() 被调用时,一个itsUpperLeft副本 被制作,并交给GetUpperLeft() 的调用者。

你还没有为class Point 定义一个拷贝构造函数(你还没有得到拷贝构造函数吗?),所以 C++ 会为你定义一个。默认的复制构造函数只会创建一个class Point 的新实例,并将itsUpperLeft 的所有数据成员的副本放入其中(在本例中为xy)。

编辑:特别是,如果调用者运行如下代码:

Point my_copy = some_rectangle.GetUpperLeft();
my_copy.SetX(13);
my_copy.SetY(12);

不会影响some_rectangle.itsUpperLeft 中的值。

【讨论】:

以上是关于使用另一个类作为返回类型的类?的主要内容,如果未能解决你的问题,请参考以下文章

类作为成员变量类型使用接口作为成员变量类型使用接口作为方法的参数或返回值使用

命名空间中的类并使用模板类型作为返回类型时的全局范围友元运算符声明

如何在 C++ 中创建一个作为另一个类的向量的类?

JavaScript中的Function类型总结

包含另一个类作为成员变量的类的构造函数

作为 Flutter 中另一个类的参数的类列表