The first class after reading the section7.1 <<C++ primer>>

本文深入探讨了C++中类的定义及其成员函数的实现方式,特别是const成员函数的作用及其实现细节。通过具体例子说明了成员函数如何访问类的成员变量,并解释了返回引用的含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Today,  I start learning the class in C++.. It's really hard to get it.So slow my step into it...

I decided to make some records at first.

The books mention a simple class on Page 73:

struct Sales_data{
 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};

then on Page256. after introducing the function. the author give a new version class.

struct Sales_data {
 // new members operations on Sales_data objects
 std::string isbn() const {return bookNo}
 Sales_data& combine (const Sales_data&);
 double avg_price() const;
 // data members are unchanged from $2.6.1(p.72)
 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};
// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istreams&, Sales_data&);

Author mentions "Functions defined in the class are implicitly inline".(p.238)

Defining Member Functions: Although every member must be declared inside its class, we can define a member function's body either inside or outside of the class body. In Sales_data, isbn is defined inside the class; combine and avg_price will be defined elsewhere.

In this class, there are three new concepts. " std::string isbn() const {return bookNo}"  the definition of isbn() behave a weird way. after the parameter list, there is a "const". what's hell the purpose of "const". Is it declarate the "{}". obviously say "no". a ghost parameter "this". "this" is a implicit behavior. In this call, when isbn returns bookNo, it's implicitly returning name.bookNo.(assume we had declarate an object <"Sales_data name">).  Member functions access the object on which they were called through an extra, implicit parameter named this. When we call a member function, this is initialized with the address of the object on which the function was invoked. For example, when we call

 name.isbn()

the compiler passes the address of "name" to the implicit thisparameter in isbn. it looks like

Sales_data:: isbn(&name)

Inside a member function, we can refer directly to the member of the object on which the function was called. we don't have to use the member access operator to use the member of the object to which this points. Any direct use of a member of the class is assumed to be an implicit reference through this. so the author tell us we can define it like that form.

std::string isbn() const{ return this->bookNo;}

It can work normally as the above definition.

the blue "const" need to catch our attention. the purpose of that const is to modify the type of the implicit this pointer.

The books give us an explanation

"""

By default, the type of this is a const pointer to the nonconst version of the class type. For example, by default, the type of this a Sales_data member function is Sales_data *const. Although this is implicit, it follows the normal initialization rules, which means that (by default) we cannot bind this to a const object. This fact, in return, means that we cannot call an ordinary member function on a const object.

If isbn were an ordinary function and if this were an ordinary pointer parameter, we would declare this as const Sales_data *const. After all, the body of isbn doesn't change the object ot which this points, so our function would be more flexible if this were a pointer to const.

However, this is implicit and does not appear in the parameter list. There is no place to indicate that this should be a pointer to const. The language resolves this problem by letting us put const after the parameter list of a memeber to const. A const following the parameter list indicates that this is a pointer to const. Member functions that use const in this way are const member functions.

"""

an interested example from the book:

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
    units_sold += rhs.units_sold; // add the member of rhs into
    revenue += rhs.revenue; // the members of "this" object
    return *this;   // return the object on which the function was called.
}

The books mentions a insterested part:

"""

The interesting ar about this function is its return type and the return statement. Ordinarily, when we define a function that operates like a built-in operator, our function should minic the behavior of that operator. The built-in assignment operators return their left-hand operand as an lvalue. To return an lvalue, our combine function must return a reference. Because the left-hand operand is a Sales_data object, the return type is Sales_data&.

As we've seen, we do not need to use the implicit this pointer to access the members of the object on which a member function is executing. However, we do need to use this to access the object as a whole:

return *this; // return the object on which the funtion was called

Here the return statement dereferences this to obtain the object on which the function is executing. That is, for the call above, we return a reference to total.

"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值