【C++二级】题一:构造函数

1、常量数据成员的初始化只能通过构造函数的成员初始化列表进行,并且要用关键字const修饰

复制代码
#include <iostream>
using namespace std;
class MyClass {
  int _i;
  friend void Increment(MyClass& f);
public:
  const int NUM;
// ERROR  ********found********
  MyClass(int i = 0):NUM{0}{
    _i = i;
  }
  int GetValue() const { return _i; }
};

2.友元函数的定义要与函数的声明相呼应,即返回值、参数类型、参数个数要一致。友元函数的调用不需要使用类名和作用域

复制代码
void Increment(MyClass& f) { f._i++; }

3.友元函数的调用不需要类名和作用域

复制代码
  Increment(obj);
相关推荐
無限進步D2 小时前
Java 运行原理
java·开发语言·入门
是苏浙2 小时前
JDK17新增特性
java·开发语言
阿里加多5 小时前
第 4 章:Go 线程模型——GMP 深度解析
java·开发语言·后端·golang
likerhood6 小时前
java中`==`和`.equals()`区别
java·开发语言·python
zs宝来了6 小时前
AQS详解
java·开发语言·jvm
telllong7 小时前
Python异步编程从入门到不懵:asyncio实战踩坑7连发
开发语言·python
And_Ii7 小时前
LCR 168. 丑数
c++
CoderMeijun8 小时前
C++ 时间处理与格式化输出:从 Linux 时间函数到 Timestamp 封装
c++·printf·stringstream·时间处理·clock_gettime
wjs20249 小时前
JavaScript 条件语句
开发语言
阿里加多9 小时前
第 1 章:Go 并发编程概述
java·开发语言·数据库·spring·golang