写c++作业过程中的问题与思考记录

函数返回值 在type后加&号的作用?

int g_test = 0;

int& getNumberReference()
{
     return g_test;
}

int getNumberValue()
{
     return g_test;
}

int main()
{
    int& n = getNumberReference();
    int m = getNumberValue();
    n = 10;
    cout << g_test << endl; // prints 10
    g_test = 0;
    m = 10;
    cout << g_test << endl; // prints 0
    return 0;
}

the getNumberReference() returns a reference, under the hood it's like a pointer that points to an integer variable. Any change applyed to the reference applies to the returned variable.

用于return type后面时,相当于return变成了一个reference,在底层它相当于一个指针。

但是!!参看C++ Returning reference to local variable - Stack Overflow

可以看出它仍不能返回local var! 

如何return local variable

1,return a value

int* fun()
{
    int x = 20;
    int* ptr = &x;
    return ptr;
}

2,return an array 

int* fun()
{
    int arr[5] = { 1, 2, 3, 4, 5 };
    int *ptr = arr;
    return ptr;
}

What about returning a local variable by value? Does the local exist as a separate object, or does it get optimized away?, C++ FAQ

参看此文,某些编译器中,return值会被当做指针。但是,如果这个函数有多个return值,则无效。

Pass by reference, 即参数中使用&(注意,int& a和int &a等效)

此时处理参数相当于处理原始被引用的参数。

可以与const配合使用。保护参数。

【为什么不直接pass by value:copy的过程可能很耗时耗空间】

This can be useful when you need to change the value of the arguments.

运算符的overload

 如果一个运算符函数是成员函数,则它的第一个(左侧)运算对象绑定到隐式的this指针上,因此,成员运算符函数的(显示)参数数量比运算符的运算对象总数少一个。 

 对于一个运算符函数来说,它或者是类的成员,或者至少含有一个类类型的参数。

 我在class外定义operator==时接连报错(声明在class内),仍不清楚为什么。【已解决,补充class::即可】此时不需要this指针,直接写变量名即可。如下。

    bool operator== (Point& otherPoint) {
        if (xValue == otherPoint.xValue && yValue == otherPoint.yValue) return true;
        else return false;
    }

在class外定义构造函数时报错

初始化const或引用类型数据成员的唯一机会是在构造函数初始化列表中。

也就是说,如果member是&型,则一开始就要初始化好构造函数。 

基于此范围的for语句需要合适的begin函数

出现在自己定义的class想使用for each处理array。

经查询,问题出现在,

对于类来说,需要有begin和end函数,范围就是begin和end之间;对于数组来说,范围就是数组的第一个和最后一个元素。
其次,还要求迭代的对象实现++和==等操作符。STL中的容器都能支持,而自己实现的类,则需要实现这些操作符。

 因此改成了普通for loop。

const Point* points是什么?

是老师写的argument,应该是一个array。看不懂 * 是怎么表示array的。

经搜索后回忆起c/cpp中数组名=pointer:

 An array name is a constant pointer to the first element of the array. Therefore, in the declaration −

double balance[50];

balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p the address of the first element of balance −

double *p;
double balance[10];

p = balance;

 因此pointer表示数组也就很自然了。

const Point*的值无法用来初始化类型 Point*

强制转换const Point*为Point*

local var 不能作为 return 值 !

必须写一个指针: int* res = &tmp, 这样才能return res。

或者直接把return type后面加上&

还不清楚operator的reload函数是否可以 。

暂时没懂

cannot bind a non-const reference to a const-object, as that would drop (discard in other compiler's errors), disregard or ignore the const qualifier.

好像大概意思是const是不能在参数之间沿用的,因此要小心一个函数用了const a, 而这个函数调用了一个non-const a作为参数的函数

函数后加const(即常量成员函数)

Constant member functions are those functions which are denied permission to change the values of the data members of their class. 

A function becomes const when the const keyword is used in the function’s declaration.  

The idea of const functions is not to allow them to modify the object on which they are called. It is recommended the practice to make as many functions const as possible so that accidental changes to objects are avoided.

简而言之,成员函数形如do() const 的意思是它不能改变成员变量。“read-only”

函数前加const:return参数

cons​​​​​​​t同理之前,和&(reference)配合使用。其实等效于直接pass by value

函数的const argument

如果有一个const变量,它只能调用声明了参数为const的函数。

如果是在class中,该const变量是member,则只能调用const function。

What are const functions in C++?

参看上文。

引号,单引号与双引号(quote)

笔者在作业中使用了char array来存储string。但在String str1{ "" }; 中遇到问题。

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.

In C++ the size of the character literal is char. In C the type of character literal is integer (int). So in C the sizeof(‘a’) is 4 for 32bit architecture, and CHAR_BIT is 8. But the sizeof(char) is one byte for both C and C++.

c++如何处理“”(empty string)

tbd

c++如何处理nullptr

nullptr在c++11出现。在c++中NULL也表示空指针,但是NULL既是指针,又被定义为0,会出现函数调用ambiguous问题。

而nullptr是扎扎实实的pointer。也就是说,它就是一个type* 。

nullptr关键字用于标识空指针,是std::nullptr_t类型的(constexpr)变量。它可以转换成任何指针类型和bool布尔类型(主要是为了兼容普通指针可以作为条件判断语句的写法),但是不能被转换为整数。

char *p1 = nullptr;     // 正确
int  *p2 = nullptr;     // 正确
bool b = nullptr;       // 正确. if(b)判断为false

operator+=

1,不能return void:否则p1+=p2后如果想继续操作??e.g. double a = (p1+=p2);

2, return type后加&,因为return *this

表达式具有类类型,但,,,

检查一下是否出现了对指针使用.运算符号。指针应该使用->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值