zzxwbs 发表于 2017-6-8 23:35:38

不是从零开始的c++学习笔记(002)——c++中的数据类型,函数重载

本帖最后由 zzxwbs 于 2017-6-8 23:43 编辑

1.结构体
1.1和c的相同点
语法上和c没有什么区别

1.2和c的不同点

1.2.1表达类型时,可以省略关键字struct
1.2.2没有任何成员变量的结构体大小是1,C语言中是0

#include <iostream>
using namespace std;
struct Fishcuser
{
       
};
int main()
{
        cout <<sizeof(Fishcuser) << endl;//1
        Fishcuser user;
        cout << sizeof user << endl;//1
}


1.2.3结构体中可以定义函数(成员函数)
在结构体成员函数中可以直接访问该结构体的成员函数,无需通过“.”或者“->”,例如
#include <iostream>
using namespace std;
struct Fishcuser
{
        char name;
        int age;
        void show()
        {
                cout << "我是" << name << "今年" << age << "岁" << endl;
        }
};
int main()
{
        Fishcuser user = {"beiai",22};
        Fishcuser *puser = &user;
        user.show();
        puser->show();
}


2.联合体

2.1和c的相同点
语法上和c没有什么不同

2.2和c的不同点

2.2.1表达类型时,可以省略关键字union
2.2.2支持匿名联合,例如

#include <iostream>
using namespace std;


int main()
{
        union
        {
                int n;
                char c;
        };
        n = 0x12345678;
        for(size_t i = 0;i <sizeof(c);i++)
        {
                printf("%#x ",c);//0x78 0x56 0x34 0x12字节序问题
        }
}


3.枚举

3.1和c的相同点
语法上和c基本没什么不同

3.2和c的不同点

3.2.1表达类型时,可以省略关键字
3.2.2一个枚举变量,可以赋值给一个整数变量,一个整数变量,不能赋值给枚举变量,c++中的类型检查要比c要严格

4.bool类型
c++中天生就支持bool类型,不需要像C语言中那样需要导入stdbool头文件
#include <iostream>
int main(int argc, const char * argv[])
{
   
    bool b;
    b = true;
    b = false;
    std::cout << std::boolalpha << b << std::endl;
    std::cout << sizeof(bool) << std::endl;
   
    b = 5;
    b = 5.5;
    b = 'a';
    std::cout << std::boolalpha << b << std::endl;
   
    return 0;
}


5.c++中的运算符转换(了解)
# ---- > %:
{      <%
&      bitand

6.c++中的函数和c函数的不同

6.1 c++中一个函数如果没有参数,则这个函数调用时严格匹配参数列表
void 类型的参数依然可用。void foo();相当于c语言中的void foo(void);

6.2不再支持c语言中的隐式声明
函数调用之前,必须进行生命或者定义

6.3不再像c函数,默认返回int类型
函数必须指定返回值类型,main函数例外

#include <iostream>
using namespace std;
void foo()
{
    cout << "foo()" << endl;
}
intgetmax(int x,int y);
//getmax(int x,int y);错误
int main()
{
    foo();
        //foo(1);错误
        //foo(1,2);错误
        cout << getmax(1,112) << endl;
}
intgetmax(int x,int y)
{   
    returnx>y?x:y;
}



7.函数重载

7.1概念
在同一作用域中,函数名相同,参数列表不同的函数构成重载关系,重载与返回类型无关,与参数名也无关,相同类型的引用与非引用不构成重载关系(关于引用可能会在下一期说明)

参数列表不同:参数的个数不同,参数的类型不同, 参数的顺序不同

调用函数时,根据实参与形参的类型匹配情况,选择一个确定的重载版本,这个过程叫做重载解析

只有同一作用域中的同名函数才涉及重载问题,不同作用域中的同名函数遵守名字隐藏原则

#include <iostream>
using namespace std;
intgetmax(int x,int y)
{
    cout << "getmax(int,int)" << endl;
        return x>y?x:y;
}
double getmax(int x,double y)
{
    cout << "getmax(int,double)" << endl;
        return x>y?x:y;
}
double getmax(double x,int y)
{
    cout << "getmax(double,int)" << endl;
        return x>y?x:y;
}
int main()
{
    getmax(1,2);
        getmax(1.5,2);
        getmax(1,2.0);
        // getmax(1.5,2.5);错误
}


7.2使用函数指针调用重载的函数
函数指针会根据指针的类型,自动选择对相应的函数

#include <iostream>
using namespace std;
intgetmax(int x,int y)
{
    cout << "getmax(int,int)" << endl;
        return x>y?x:y;
}
double getmax(int x,double y)
{
    cout << "getmax(int,double)" << endl;
        return x>y?x:y;
}
double getmax(double x,int y)
{
    cout << "getmax(double,int)" << endl;
        return x>y?x:y;
}
int main()
{
    getmax(1,2);
        getmax(1.5,2);
        getmax(1,2.0);
        // getmax(1.5,2.5);
        /* 使用函数指针来调用 第二个函数 */
    double (*pfun) (int x,double y);
        pfun=getmax;
        pfun(1,1.5);
        double (*pfun2)(double x,int y);
        pfun2=getmax;
        pfun2(2.5,30);
}

7.3重载的原理
c++ 语言底层在生成调用函数名时,不但要考虑函数,还要考虑参数列表,而c语言只考虑函数名。所以c++函数可以重载,而c语言函数不能。

重载是通过c++换名实现的,换名机制限制了c和c++模块之间的交叉引用

#include <iostream>
using namespace std;
/* 底层实现时函数真正的名字_Z6getmaxii */
intgetmax(int x,int y)
{
    returnx>y?x:y;
}
/* 底层实现时函数真正的名字_Z6getmaxid*/
double getmax(int x,double y)
{

}
int main()
{
    getmax(1,2);
        getmax(1,2.5);
}
#include <stdio.h>
//C语言中底层实现时函数名就是getmax
intgetmax(int x,int y)
{
    returnx>y?x:y;
}

int main()
{
    getmax(1,2);
}

7.4重载引入的问题,以及如何解决

7.4.1跨编译器调用会有问题
7.4.2使用extern "C" 指定函数按照c语言的方式来生成调用函数名

#include <iostream>
using namespace std;
/* 按照c语言的方式来生成调用函数名 */
// extern "C" intgetmax(int x,int y);
extern "C"
{
    intgetmax(int x,int y);       
}
int main()
{
    getmax(100,200);
}


8.函数参数之哑元

8.1概念
如果一个函数的参数,只有参数类型,没有参数名,则这个参数称之为哑元

8.2作用

8.2.1让无参的参数列表严格匹配
8.2.2保持函数的向前兼容特性
例如加密解密算法函数中,新算法函数无需key参数,又需要和以前的函数兼容,可以使用哑元参数

const char* encode(const char* msg,int key);
const char* decode(const char* msg,int key);

const char* encode(const char* msg,int key);
const char* decode(const char* msg,int);

8.2.3区分成员函数,以后会细说
#include <iostream>
using namespace std;
structDate
{
        /* 成员变量 */
    intyear;
        intmonth;
        intday;
        /* 默认前++ 需要使用 哑元区分后加加 */
    Dateoperator++(){}
        Dateoperator++(int){}
};
int main()
{
    Datedate={2015,7,7};
    ++date;
        date++;
}

下期预告:函数参数的默认值,内联函数,c++中的动态内存分配,引用

xiaosanxian 发表于 2017-6-18 00:20:18

挺好的,支持
页: [1]
查看完整版本: 不是从零开始的c++学习笔记(002)——c++中的数据类型,函数重载