c++名字查找和作用域的一个例子的感想
发布日期:2021-05-09 20:12:20 浏览次数:19 分类:精选文章

本文共 3201 字,大约阅读时间需要 10 分钟。

���������������������C++������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������_test.h���������

���_test.h���������������Test���������������

class Test {  
public:
const static int sta_int = 1234;
using pos = size_t;
int big_ball = 1234;
Test() = default;
Test(int k1, int k2): i1(k1), j(k2) {};
void print() const;
void fun(Test &t);
void fun(int k);
private:
int i1;
int j;
};

������������������������������������������i1���j������������������������������������������������������������static const������sta_int���������������������������������������big_ball���������������������������������

���setup.cc������������������������������������������

void Test::print() const {  
std::cout << "Test class variable: " << i1 << std::endl;
std::cout << "Global variable: " << ::i << std::endl;
}
void Test::fun(Test &t) {
if (i1 > 0) {
std::cout << "Private data sum: " << i + t.i1 << std::endl;
}
if (j > 0) {
std::cout << "Private data sum: " << j + t.j << std::endl;
}
Test::pos p1 = 111;
}

���������������print()���fun()������������������������������������fun()������������������������������Test������t���������������������������

���main.cc���������������Test������������������������

#include "test.h"  
int main() {
Test t1(1, 2);
Test t2(100, 200);
Test::pos p1 = 111, p2 = 222;
t1.print();
t1.fun(t2);
t1.fun(1234);
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
std::cout << Test::sta_int << std::endl;
return 0;
}

������������������������������������Test������t1���t2������������������print()���fun()������������������������������������������������������������������������

������������������������������������������������inside���������������������������������������Test::fun()������������������������������������������������������������Test������t���������������������������������k������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������

  • ������������������������������������
  • ������������������������������������������������
  • ������������������������������������
  • ������������������������������������������������������������������������������global::������������

    ������������������������������������������������������������������������������������������������������������������::������������������������������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������_class definition_������������������������������������������������������������������������������������C++��������������������������������������������������������������������������������������������������� reflex���

    上一篇:IDEA不能pull报错:Could not read from remote repository
    下一篇:第七章_类_7.4 类的作用域(加了自己的总结部分)

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年04月07日 12时02分28秒