C++对C的扩展(冒号作用域、命名空间、const的区别)

C++ hello world

#include <iostream>  // 头文件  标准的输入输出流

using namespace std;  // 声明了一个命名空间 cout对象是存在std作用域下


int main(int argc, char *argv[])
{
    // cout 标准输出流
    // endl 换行
    cout << "Hello World!" << endl;
    return 0;
}

冒号作用域

::运算符是一个作用域,如果::前面什么都没有加,代表是全局作用域

#include <iostream>
using namespace std;

int a = 100;
void test01()
{
    int a = 10;
    cout << a << endl;  // 输出局部变量
    cout << ::a << endl; // 输出全局变量
}
int main()
{
    test01();
    return 0;
}

命名空间的使用

命名空间下可以存放:变量 函数 结构体 类

#include <iostream>

using namespace std;

// 命名空间的定义 必须定义在全局范围
// 命名空间下可以存放 变量 函数 结构体 类
namespace A
{
    int a = 10;
    void fun()
    {
        cout << "hello"  << endl;
    }
    void foo(int arg);
    struct std
    {};
    class obj
    {};
}
void A::foo(int arg)
{
    cout << arg  << endl;
}

namespace B
{
    int a = 20;
    int b = 22;
}

int a = 100;
void test01()
{
    int a = 10;
    cout << a << endl;  // 输出局部变量
    cout << ::a << endl; // 输出全局变量
}

void test02()
{
    cout << A::a <<endl;
    cout << B::a <<endl;
    cout << B::b <<endl;
    A::fun();
    A::foo(222); 
}

int main()
{
    // test01();

    test02();
    return 0;
}

c和c++里面的const

  • c语言中
    1、在c语言中,const修饰的局部变量,存在栈区。虽然不能通过const修饰的变量去修改栈区内容,但是可以通过地址去修改
    2、const修饰的全局变量是保存在常量区,不能通过变量名去修改,也不能通过地址去修改
    3、const修饰的全局变量,如果其他文件想使用,直接extern声明外部可用即可

test.c的内容

// 

const int num = 1;

main.c的内容

// 

#include <stdio.h>

const int b = 10;  // const修饰的全局变量保存在常量区

void test03()
{
    extern const int num; // 声明num是外部的
    printf("num = %d\n", num);
}

void test02()
{
    int *p = &b;
    // *p = 100;     // 这里是错误的 不能修改常量区的内容
    // printf("b = %d\n",b);
}



void test01()
{
    const int a = 10;  // const修饰的局部变量,保存在栈区
    // a = 100;
    int *p = &a;
    *p = 100;
    printf("a = %d\n",a);
}

int main()
{

    test03();
    test02();
    test01();

    return 0;
}

  • c++里面
    1、c++中const修饰的局部变量,存在符号表中,修改不了,是个常量
    2、const修饰的全局变量,存在常量区,不能修改
    3、const修饰的全局变量默认是内部链接属性,加extern才是外部链接属性

test.cpp

extern const int num = 109;  
// const修饰的全局变量默认是内部链接属性,加extern才是外部链接属性

main.cpp

#include <iostream>

using namespace std;


void test03()
{
    extern const int num;
    cout << num << endl;    // 109
}


const int b = 1;
void test02()
{
    // const修饰的全局变量,存在常量区
    int *p = (int *)&b;
    // *p = 100;        // 错误的 修改不了

    cout << b << endl;
}


// c++中const修饰的局部变量
void test01()
{
    // c++中const修饰的局部变量,存在符号表中
    const int a = 10;
    // a = 100;  // 错误

    // 对const修饰的局部变量取地址 编译器会产生一个临时变量来保存a的地址
    // int tmp = a;  int *p = &tmp;
    int *p = (int *)&a;
    *p = 100;

    cout << *p << endl;   // 100
    cout << a << endl;    // 10
}

int main()
{
    test01();
    test02();
    test03();

    return 0;
}

c++里面const举例

1、c++中const修饰的局部变量赋值为常量时,存在符号表中
2、c++中const修饰的局部变量a,被赋值变量b时,局部变量a存在栈区

3、const 修饰的变量为自定义变量 ( struct ) 时,保存在栈区

#include <iostream>

using namespace std;



// c++中const修饰的局部变量
void test01()
{
    // c++中const修饰的局部变量,存在符号表中
    const int a = 10;
    // a = 100;  // 错误

    // 对const修饰的局部变量取地址 编译器会产生一个临时变量(分配一个临时空间)来保存a的地址
    // int tmp = a;  int *p = &tmp;
    int *p = (int *)&a;
    *p = 100;

    cout << *p << endl;   // 100
    cout << a << endl;    // 10
}


// c++中const修饰的局部变量
void test0111()
{
    // c++中const修饰的局部变量a,被赋值变量b时,局部变量a存在栈区
    int b = 101;
    const int a = b;
    cout << a << endl;    // 101

    int *p = (int *)&a;
    *p = 100;

    cout << *p << endl;   // 100
    cout << a << endl;    // 100
}



struct stu
{
    int a;
    int b;
};
void test03()
{
    // const 修饰的变量为自定义变量时,保存在栈区
    const struct stu obj  = {1,2};
    struct stu *p = (struct stu *)&obj;
    p->a = 3;
    p->b = 4;
    cout << obj.a << " " << obj.b << endl;    // 3 4
}

int main()
{
    test01();
    test0111();

    test03();

    return 0;
}

引用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值