C++对C的增强

1 c++语法检查增强

1.1 全局变量检测增强

#include <stdio.h>

int a = 10;  // 赋值,当作定义
int a;  // 没有赋值,当作声明

// c++中会出现error: redefinition of 'int a'

int main(int argc, char *argv[])
{
    printf("a:%d
", a); 
    return 0;
}

上示代码在c++下编译失败,在c下编译通过。

1.2 变量和函数都必须有类型

c语言中如果没有声明函数形参的类型时,可以接收任意类型的实参。而在c++中函数的声明与定义必须给出函数形参的类型,否则无法编译通过。

#include <stdio.h>

// i没有声明类型,可以是任意类型
int fun1(i)
{
    printf("%d
", i);
    return 0;
}

int fun2(i)
{
    printf("%s
", i);
    return 0;
}

// 没有参数的函数,代表可以接收任何类型的实参
int fun3()
{
    printf("i'm fun3
");
    return 0;
}

// c语言中如果一个函数没有参数,建议写void,代表没有参数
int fun4(void)
{
    printf("i'm fun4
");
    return 0;
}

g()
{
    return 10;
}

void test()
{
    fun1(10);
    fun2("viktor");
    fun3(1, 2, "viktor");
    fun4();
    printf("g = %d
", g());
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

上示c代码c编译器编译可以通过,c++编译器无法编译通过。

在c语言中,int func()表示返回值为int,接收任意参数的函数;int func(void)表示返回值为int的无参函数。

在c++中,int func()int func(void)具有相同的意义,都表示返回值为int的无参函数。

1.3 更严格的类型转换

在c++中不同类型的变量一般是不能直接赋值的,需要进行强制类型转换。

#include <stdio.h>

typedef enum COLOR{ GREEN, RED, YELLOW } color;

void test()
{
    color mycolor = GREEN;
    mycolor = 10;
    printf("mycolor:%d
", mycolor);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

上示c代码c编译器编译可通过,c++编译器无法编译通过。

2 struct类型加强

c语言中结构体变量需要加上struct关键字,c++中不需要;

c语言中的结构体只能定义成员变量,不能定义成员函数,而c++中二者都可以定义。

#include <iostream>
using namespace std;

// c++结构体中既可以定义成员变量,也可以定义成员函数
struct Student{
    string mName;
    int mAge;
    void setName(string name) { mName = name; }
    void setAge(int age) { mAge = age; }
    void showStudent(){
        cout << "Name:" << mName << endl << "Age:" << mAge << endl;
    }
};

void test03()
{
    // c++定义结构体变量不需要加struct关键字
    Student stu1;
    stu1.setName("Tom");
    stu1.setAge(22);
    stu1.showStudent();
}

int main(int argc, char *argv[])
{
    test03();
    return 0;
}
#include <stdio.h>

void test03()
{
    // c定义结构体变量必须加struct关键字
    struct stu stu1 = {20, "tom"};
    printf("stu1的年龄:%d
", stu1.age);
    printf("stu1的姓名:%s
", stu1.name);
}

int main(int argc, char *argv[])
{
    test03();
    return 0;
}

3 新增bool类型关键字

标准c++bool类型有两种内建的常量true(转换为整数1)和false(转换为整数0)表示状态。

bool true false都是关键字。bool类型只有两个值,true(1值),false(0值)。

bool类型占1个字节大小,给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)。

#include <iostream>
using namespce std;

void test()
{
    cout << "false:" << false << endl;
    cout << "true:" << true << endl;
    cout << sizeof(false) << endl;  // 为1,bool类型占一个字节大小
    bool flag = true;
    flag = 100;  // 给bool类型赋值时,非0值会自动转换为true(1),0值会自动转换false(0)
    cout << "flag:" << flag << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

c语言中也有bool类型,在c99标准之前没有bool关键字,c99标准已经有bool类型,包含头文件stdbool.h,就可以使用和c++一样的bool类型。

4 三目运算符功能增强

  • c语言三目运算表达式返回值为数据值,为右值,不能赋值
#include <stdio.h>
void test()
{
    int a = 10;
    int b = 20;
    printf("ret: %d
", a > b ? a : b);

    // (a > b ? a : b) = 100; // error: lvalue required as left operand of assignment
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}
  • c++语言三目运算表达式返回值为变量本身(引用),为左值,可以赋值
#include <iostream>
using namespace std;

void test()
{
    int a = 10;
    int b = 20;

    cout << "ret: " << (a > b ? a : b) << endl;
    cout << "b: " << b << endl;
    (a > b ? a : b) = 100;
    cout << "b: " << b << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

左值和右值概念:在c++中可以放在赋值操作符左边的是左值,可以放到赋值操作符右边的是右值。有些变量既可以当左值,又可以当右值。左值为Lvalue,L代表Location,表示内存可以寻址,可以赋值。右值为Rvalue,R代表Read,表示该变量只读。比如:int temp = 10;temp在内存中有地址,10没有地址,但是可以访问到其值。

原文地址:https://www.cnblogs.com/dabric/p/14635796.html