c++基础

1. 二级指针

int main() {

    int (*p)[3];
    int arr[2][3] = {{3,2,1},{4,5,6}};

//    int arr[3] = {1,2,3};
//    for (int i = 0; i < 3; ++i) {
//        p[i] = arr;
//    }

    p = arr;
//    int arr2[] = {4,5,6};
//    p[1] = arr2;

    printf("%d",**p);
    printf("%d",*(*p+1));
    printf("%d",*(*p+2));

    printf("%d",*(*(p+1)+1));
    printf("%d",*(p[1]+2));


    for (int i = 0; i < 2; ++i) {
        printf("\nindex %d\n",i);
        for (int j = 0; j < 3; ++j) {
//            printf("arr %d  ",*(*(p+i)+j));
            printf("arr %d  ",(*(p+i))[j]);
        }
    }


    return 0;
}

2.结构体

//结构体使用 People与Student区别  People是一个全局变量 Student是一个结构体类型
struct _People{
    char* name;
    int age;
}People;

typedef _People ZZ;

typedef struct _Student{
    char* name;
    int age;
}Student;

int main() {

//    ZZ a;
//    struct _People a{"zz",12};
//    a.age = 25;
//    a.name = "dabaicai";
//    printf("a name is %s age is %d",a.name,a.age);

//    People.name = "dabaicai";
//    People.age = 25;
//    printf("People name is %s age is %d",People.name,People.age);

//    struct _Student ss{"aa",12};
    Student ss;
    ss.name = "dabaicai";
    ss.age = 25;
    printf("ss name is %s age is %d",ss.name,ss.age);

    return 0;
}

3.类

-----------------------------------h----------------------------------------------
#ifndef UNTITLED_STUDENT_H
#define UNTITLED_STUDENT_H


class Student {
private:
    static Student *instance;
    Student();

    int age;
public:
    char *name;

    Student(int age);

    static Student *getInstance();

    void friend setAge(Student *student);

    Student operator+(const Student &student) {
        Student temp;
        temp.age = this->age + student.age;
        return temp;
    };

    int getAge(){
        return age;
    }
};


#endif //UNTITLED_STUDENT_H


-----------------------------------cpp----------------------------------------------



Student* Student::instance = 0;
Student*Student::getInstance() {
    if(!instance){
        instance = new Student();
    }
    return instance;
}

Student::Student() {

}

Student::Student(int age) {
    this->age = age;
}


-----------------------------------main---------------------------------------------


#include 
#include "Student.h"

//友元函数
void setAge(Student * student){
    student->age = 25;
}

int main() {


    Student* student = Student::getInstance();
    setAge(student);


    Student* student2 = new Student(50);
    Student student1 = *student + *student2;
    printf("student age is %d",student1.getAge());
    return 0;
}

4.模板

#include 


template
class Q {
public:
    T test(T t, E e) {
        return t + e;
    }
};

template
T zz(T a, T b) {
    return a + b;
}


int main() {

    Q q;
    std::cout << q.test(1, 1.5f) << std::endl;

    std::cout << zz(1, 4);

    return 0;
}

5.命名空间

可以区分二个同名函数

#include 


template
class Q {
public:
    T test(T t, E e) {
        return t + e;
    }
};

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

float add(float a, float b) {
    return a + b;
}

template
T zz(T a, T b) {
    return a + b;
}


namespace first_space{
    void func(){
        printf("first space func");
    }
}

namespace second_space{
    void func(){
        printf("second space func");
    }
}


int main() {

    first_space::func();
    second_space::func();

    int i = 1;
    int&b = i;
    int c = i;
    printf("\ni is %d,&b is %d,c is %d",&i,&b,&c);
    //i is -274684040,&b is -274684040,c is -274684052

    return 0;
}

6.gcc编译四步骤

1.预处理( Pre-Processing ):将要包含(include)的文件插入原文件中、将宏定义展开、根据条件编译命令选择要使用的代码
gcc -E -o add.i add.c
2.编译( Compiling ):检查代码规范,错误等后将代码翻译成汇编代码
gcc -S -o add.s add.i
3.汇编( Assembling ):生成目标文件.o文件,将汇编代码生成目标(机器)代码
gcc -c add.cpp -o add.o (最耗时)
4.链接( Linking ):将汇编生成的OBJ文件、系统库的OBJ文件、库文件链接起来,最终生成可以在特定平台运行的可执行程(文件)
gcc add.o -o add (生成可执行文件add)

7.生成静态库

ar 将目标文件打包成静态库
ar t xxx.a 查看静态库内容

//add.h
int add(int a,int b);

//add.c
#include "add.h"

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

//main.c
#include
#include "add.h"

int main(){
    int a = add(1,2);
    printf("a = %d",a);
    return 0;
}

1. gcc -c add.c //将 add.c 编译成 add.o
2. ar rcs libadd.a add.o //将 add.o 生成静态库文件
3. gcc -o main main.c -L. -ladd // 将 main.c 编译并链接 libadd.a 库文件

---------------------------------------------
执行 ./main 
输出 a = 3

8.生成动态库

1. gcc -c add.c //将 add.c 编译成 add.o
2.gcc -shared -fPIC -o libadd.so add.o //生成动态库
3.gcc -o main main.c -L. -ladd // 将 main.c 编译并链接 libadd.so 库文件

---------------------------------------------
linux下执行 LD_LIBRARY_PATH=. ./main 
mac下执行 ./main
输出 a = 3

---------------------------------------------
linux 执行 ldd main
    linux-vdso.so.1 =>  (0x00007ffe8aeb9000)
    libtool.so => not found
    libc.so.6 => /lib64/libc.so.6 (0x00007f2c0c74b000)
    /lib64/ld-linux-x86-64.so.2 (0x0000555dd56da000)

注意:在linux上直接执行 ./main 会报 ./main: error while loading shared libraries: libtool.so: cannot open shared object file: No such file or directory 这个错误 ,原因是生成的动态库在默认查找的动态库目录下没有(可以用 ldd main 查看查找动态库目录),所以要设置查找目录。
而在 mac 下直接执行不会报,目前不清楚。。

你可能感兴趣的:(c++基础)