简体   繁体   English

C ++:将“ this”指针与“ get”函数一起使用以替换Cout语句的可能性

[英]C++: Possibilities For Using the 'this' Pointer With “get” Functions to Replace Cout Statements

After just learning how to cascade multiple mutator function calls by defining those functions with return type "reference to an object of a class C" (ie, C &setHeight( int ) ) in conjunction with corresponding 'return *this' statements, I would very much like to know if a similar method allows an object's data members to be returned from a single "get" function. 在通过定义返回类型为“对类C的对象的引用”(即C&setHeight(int))并结合相应的'return * this'语句定义了那些级联多个mutator函数调用之后,我会非常想知道类似的方法是否允许从单个“ get”函数返回对象的数据成员。
I have a simple "invoice" class, with an object from another class, "Date". 我有一个简单的“发票”类,其中有另一个类“日期”中的对象。 This data member object itself contains private data that store the standard date for the "date of purchase" of a hypothetical product (for which the object was created). 该数据成员对象本身包含私有数据,这些数据存储假设产品(为其创建对象)的“购买日期”的标准日期。 Although in "Date", three int members store the date components, I would like to somehow cascade calls to the three corresponding get functions, belonging to the Date object, in my "date of purchase" accessor, as some single accessor/get appears to be required, and because this style seems preferable to completing the task with a cout statement. 尽管在“日期”中,三个int成员存储了日期成分,但我想以某种方式在我的“购买日期”访问器中级联调用属于Date对象的三个对应的get函数,因为出现了单个访问器/获取是必需的,因为这种样式似乎比使用cout语句完成任务更可取。 In other words, if some get function must be used to display the full date, I'd rather it were displayed formatted, with a single call within a print statement in the client (function main), than by a function that merely prints a formatted statement when called from main. 换句话说,如果必须使用一些get函数来显示完整日期,我宁愿将其显示为格式化的,而不是通过仅在客户端上的print语句(函数main)中的一次调用客户端(函数main)中进行显示。从main调用时的格式化语句。

//Invoice.h

#ifndef DATE_H
#define DATE_H

#include "Date.h"
...using namespace std. (Assume all other necessary libraries, classes, are included)

class Invoice {

public:

Invoice(); // Default constructor
Invoice( Date &, string );
setDateOfPurchase( int, int, int );
getDateOfPurchase();

private:

Date DateOfPurchase;
string description;

}; // end class definition

#endif

Member function definition below... 下面的成员函数定义...

//Invoice.cpp
#include "Date.h"
(Again, please assume the necessary inclusions are made here)


Invoice::Invoice(){ 

DateOfPurchase.month = 1;
DateOfPurchase.day = 1;
DateOfPurchase.year = 1999;
} // end Default constructor definition

Invoice::Invoice( Date &purchaseDate, string desc ):
DateOfPurchase( purchaseDate ),
description( desc )
{} // No body

Invoice &Invoice::setDateOP( int m, int d, int y ) {
// The Date class definition is not explicitly defined; please assume these simply accept
// one int argument each.
DateOfPurchase.setMonth( m );
DateOfPurchase.setDay( d );
DateOfPurchase.setYear( y );

return *this;
} // End mutator definition

The header for my accessor function intended to retrieve the full date stored in object DateOfPurchase looks like the following: 我的访问器函数的标题旨在检索存储在对象DateOfPurchase中的完整日期,如下所示:

Invoice &Invoice::getDateOrd(){

//gets month, day, and year, possibly through utilizing dereferenced 'this' pointer (*this)

} // end getDateOrd definition

function main below... 下面的功能主要...

In main: // Creation of a Date object, which will be passed by reference to the invoice constructor. 在main中://创建一个Date对象,该对象将通过引用传递给发票构造函数。 Date today( 11, 10, 2014 ); 今天(2014年11月10日);

Invoice firstSale( today, A corkscrew: product# 88090 );

// Cascading call to each member function of *member data object* DateOfPurchase
firstSale.setMonth( 11 ).setDay( 12 ).setYear( 2014 );   

Hopefully, this demonstrates a basic application of the cascading call with "set" functions. 希望这演示了带有“设置”功能的级联调用的基本应用。 Now for "get" functions... (By the way, I'm using Deitel & Deitel, 2013, 7th edition) 现在使用“获取”功能...(顺便说一句,我使用的是Deitel&Deitel,2013年,第7版)

Use << operator overloading in the Invoice class 在Invoice类中使用<<操作符重载

add the below code into your Invoice class. 将以下代码添加到您的Invoice类中。

friend ostream &operator<<( ostream &output,
                                 const Invoice  &invoice )
{
    output << "F : " << invoice.DateOfPurchase.dd << " " << invoice.DateOfPurchase.mm<<" "<<invoice.DateOfPurchase.yy;
   return output;
}

You can directly call as 您可以直接致电为

Invoice invoice;
cout<<invoice;

Hope it will solve your problem. 希望它能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM