C++学习笔记——(四)可变参数、static关键字、this关键字、友元函数和友元类

注:编码工具是CLion+Cygwin64

目录

可变参数

static关键字

非类中static变量和函数

类中的static变量和函数

this关键字

友元

友元函数

友元类


可变参数

#include <iostream>
#include <cstdarg>

using namespace std;

void test(int count, ...) {
    // 第一步
    va_list vl;
    // 第二步
    va_start(vl, count);
    int i;
    // 第三步
    for (i = 0; i < count; i++) {
        char *str = va_arg(vl, char*);
        cout << str << " ";
    }
    // 第四步
    va_end(vl);
}

int main() {
    test(3, "他", "她", "它");
    return 0;
}

输出:

他 她 它 

static关键字

非类中static变量和函数

#include <iostream>

using namespace std;

static int staticVar = 200;

static int add(int a, int b){
    return a + b;
}

int main(){
    cout << staticVar << endl;
    int result = add(30, 90);
    cout << result << endl;
    return 0;
}

输出:

200
120

类中的static变量和函数

        类中的static变量必须在类中声明,在类外赋值,否则报错,const变量除外。

        类的static变量和函数可以用 类名:: 形式访问,也可以用 对象名. 形式访问。

        static函数只能访问static变量,不能访问非static变量。

#include <iostream>

using namespace std;

class Static{
public:
    static const int num = 10000;
    // static int num2 = 100; // Non-const static data member must be initialized out of line
    // 先声明
    static int num2;
    char * description;

    static void show(){
        // Invalid use of member 'description' in static member function
        // cout << "description = " << description << endl;
        cout << "num = " << num << endl;
        cout << "num2 = " << num2 << endl;
    }
};

// 再赋值
int Static::num2 = 100;

int main(){
    Static::show();
    Static aStatic;
    // aStatic.num = 100; // Cannot assign to static data member 'num' with const-qualified type 'const int'
    aStatic.num2 = 100000;
    aStatic.description = "测试static";
    cout << aStatic.description << endl;
    aStatic.show();
    return 0;
}

输出:

num = 10000
num2 = 100
测试static
num = 10000
num2 = 100000

this关键字

        类中的this默认是指针常量,不能修改其所存的内存地址值,只能修改其所存的内存地址里面所存放的部分值(为什么是部分呢?因为类的某些成员变量时const的)。

        如果在类的某些函数声明最后加const,则在这些函数中,this就变成了常量指针常量了,在这些函数中,只能读取成员变量的值,不能修改了(static变量除外)。

#include <iostream>

using namespace std;

class ConstThis {
public:
    char *str;
    int i;
    static char character;

    // 可读可改成员变量
    void update() {
        str = "updated";
        i = 101;
    }

    // 加const之前,this是 ConstThis * const类型
    // 加了const this变成了 const ConstThis * const类型
    void update2() const {
        // str = "Punch";// Cannot assign to non-static data member within const member function 'show'
        character = 'F';// static变量可以改
    }

    void show() const {
        cout << "str = " << str << endl <<
             "i = " << i << endl <<
             "character = " << character << endl;
    }
};

char ConstThis::character = 'A';

int main() {
    ConstThis constThis;
    constThis.str = "before";
    constThis.i = 1;
    constThis.show();
    cout << "---------------" << endl;
    constThis.update();
    constThis.update2();
    constThis.show();
    return 0;
}

输出:

str = before
i = 1
character = A
---------------
str = updated
i = 101
character = F

友元

友元函数

        类的友元函数可以访问类的所有成员,无论是public的还是非public的。

        友元函数定义方式,就是在类中用friend声明函数,在类外面实现函数。

#include <iostream>

using namespace std;

class FriendFunction {
private:
    char *str;
    int num;

public:
    FriendFunction(char *str, int num) : str(str), num(num) {}

    char *getStr() {
        return str;
    }

    int getNum() {
        return num;
    }

    friend void update(FriendFunction *friendFunction, char *s, int i);

    void show() const{
        cout << "str = " << str << ", num = " << num << endl;
    }
};

void update(FriendFunction *friendFunction, char *s, int i) {
    friendFunction->str = s;
    friendFunction->num = i;
}

int main() {
    FriendFunction friendFunction("友元函数测试", 99);
    friendFunction.show();
    update(&friendFunction, "用有缘函数修改了类成员", 100);
    friendFunction.show();
    return 0;
}

输出:

str = 友元函数测试, num = 99
str = 用有缘函数修改了类成员, num = 100

友元类

        在友元类中可以访问另一个类的所有成员,无论是public的还是非public的,但必须先持有该类的对象。

#include <iostream>

using namespace std;
class FriendClass;
class Source{
private:
    int x,y;
    friend class FriendClass;
public:
    Source(){
        x = 10;
        y = 10;
    }
};

class FriendClass {
private:
    Source source;
public:
    void update(int x, int y){
        source.x = x;
        source.y = y;
    }

    void show() const{
        cout << "   source.x = " << source.x << ", source.y = " << source.y << endl;
    }
};

int main() {
    FriendClass friendClass;
    cout << "source默认值:" << endl;
    friendClass.show();
    friendClass.update(100, 100000);
    cout << "source修改后值:" << endl;
    friendClass.show();
    return 0;
}

输出:

source默认值:
   source.x = 10, source.y = 10
source修改后值:
   source.x = 100, source.y = 100000

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值