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

时间:2013-12-24 15:43:36

标签: c++ class methods

我一直在阅读关于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: }

2 个答案:

答案 0 :(得分:2)

itsUpperLeft是类Point的一个实例。

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

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

答案 1 :(得分:0)

调用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中的值没有影响。