【C++学习】知识点

1. c 与c++ string

	C 中char数组   C++可使用string 

2.c与c++ struct

	C中 命名为 struct  className  varName;
	C++ 可省略struct  直接  className  varName;

3.指针delete的使用

	- delete只能用于释放new分配的内存
	- new [] 为数组分配,需使用delete[] 去释放。
	-  new [] 为实体分配,需使用delete去释放。
    int *a = new int;
    delete a ;// OK
    delete a ;//not ok 不要尝试释放 释放过的指针,会引起不可预知的错误。
    int b = 5;
    int *c = &b;
    delete c;//not ok, delete只适用于释放new 分配的内存

4.c++结构体指针跟非指针的访问

int main()
{
    using namespace std;
    struct things {
        int good;
        int bad;
    };
    things grubnose = {3,400};
    things * pt = &grubnose;
    cout << grubnose.bad << ";" << grubnose.good << endl;
    cout << pt->bad << ";" << pt->good << endl;
    cout << "-----------" << endl;
    grubnose.good = 100;
    cout << grubnose.bad << ";" << grubnose.good << endl;
    cout << pt->bad << ";" << pt->good << endl;
    cout << "-----------" << endl;

    pt->bad = 500;
    cout << grubnose.bad << ";" << grubnose.good << endl;
    cout << pt->bad << ";" << pt->good << endl;
    cout << (*pt).bad << ";" << (*pt).good << endl;
    cout << "-----------" << endl;


    return 0;
}

结果:

400;3
400;3
-----------
400;100
400;100
-----------
500;100
500;100
500;100
-----------

5.C++分配内存的方法

  • 自动存储:函数内部成员变量,函数执行完毕自动释放。
  • 静态存储:函数外定义,或使用static定义,运行过程中全程占用内存。
  • 动态存储:new delete 运算符,new 需对应一个delete,否则易导致内存泄漏。
  • 线程存储:thread_local 修饰 线程结束时被销毁

6.char 数组自己命名时需以’\0’结尾

示例: char a[] = {‘a’ , 'b' , 'c' ,'\0'};  通过strlen  拿到的数组长度为3 ;		  

7.别名的两种方式

#define BYTE char;       byte为char的别名。
typedeof char byte;    byte为char的别名。

8.CCTYPE库中的函数以及返回值

在这里插入图片描述

9. c++ function中的数组参数其实是指针,所以无法通过sizeof拿到具体长度。

示例:
int calcArray(int b [],int count){
    cout << "b size = " << sizeof b << endl;
    int sum = 0;
    for (int i = 0; i < count; i++)
    {
        sum += b[i];
    }
    return sum;
    
}


int main()
{
    const int NUM = 4;
    int a[NUM] = {1,2,3,4};
    cout <<"a size = " << sizeof a << endl;
    int sum = calcArray(a,NUM);
    cout << "sum = " << sum << endl;
    return 0 ;
}

结果为:

a size = 16
b size = 8
sum = 10

11.const的一些运用

  • 指针
    int a = 10;
    const int *pt = &a ;//指针pt不可以修改a的值,但是可以更换指向地址,即不可以 *pt = 20 ,但可以 pt = &b;
    int * const pt = &a ;//指针pt可以修改a的值,但是不可以更换指向地址,即可以 *pt = 20 ,但不可以 pt = &b;
    const int * const pt = &a ;//指针pt即不可以修改a的值,也不可以更换指向地址
  • 方法
        int queueCount() const;//此方法实现内不允许修改成员变量。
        bool enQueue(const Item & item);//只有读item的权限
        const Item get();//只有Item返回的读权限

12.C++中除数组默认为指针传递外,其他结构默认均为值copy传递(即在方法内修改不会影响原始数据的值)

引用传递,方法内修改也会影响原始数据值。

引用传递时需注意参数跟传递内容需类型一致,否则可能无法达到预期效果 \color{#FF0000}{引用传递时需注意参数跟传递内容需类型一致,否则可能无法达到预期效果} 引用传递时需注意参数跟传递内容需类型一致,否则可能无法达到预期效果

13.C++中为方法参数设置默认值只能从右往左设置

void test(int a,int b,int c = 10);//valid
void test(int a = 9,int b = 10,int c = 10);//valid
void test(int a,int b = 10,int c);//Invalid
void test(int a = 10,int b ,int c);//Invalid

14.C++11 explicit 强制显示修饰符可用于转换函数。

15.C++ 涉及统计现存实例个数,需重写构造函数,复制构造函数,赋值构造函数,

private:
    static int num_strings;//统计实例个数
StringBad::StringBad(const char * s ){//构造
    num_strings++;
}

StringBad::StringBad() : StringBad("def"){//构造
}
StringBad::StringBad(const StringBad & s){//复制构造
    num_strings++;
}
StringBad & StringBad::operator=(const StringBad &s){//赋值
    num_strings++;
}

如不想重写赋值,复制构造函数,可将其放入私有模块。

16. virtual 修饰符的使用

类集成场景:如不加virtual,指针调用方式时将默认调用指针类型的犯法:
.h:

class Animal
{
private:
protected:
    string name;
    
public:
    Animal(const string & n) : name(n){
        cout << "Animal Construct method" << endl;
    };
    ~Animal(){};
    virtual void cry(){
        cout << "Animal cry" << endl;
    }
    void eat(){
        cout << name << " is eating fast" << endl;
    }
};


class Elephant : public Animal
{
private:
    int noseLength;
public:
    Elephant(int noseLength ) : Animal("Elephant"){
        Elephant::noseLength = noseLength;
        cout << "Elephant is call; length" << Elephant::noseLength << endl;
    }
    ~Elephant(){
    };
    virtual void cry(){
        cout << "Elephant cry" << endl;
    }
    void eat(){
        cout << name << " is eating slowly" << endl;
    }
    void run(){
        cout << "I am an elephant , I'm running" << endl;
    }
};

.cpp:

int main()
{
    Animal bird = Animal("Bird");
    Elephant el = Elephant(18);

    Animal * a1, * a2;
    a1 = &bird;
    a2 = &el;
    a1->eat();
    a2->eat();
    a1->cry();
    a2->cry();

}


out:
"d:\UnRealWorkSapce\CplusStudy\chapter13\extend\"testmain
Animal Construct method
Animal Construct method
Elephant is call; length18
Bird is eating fast
Elephant is eating fast
Animal cry
Elephant cry

可以发现 使用了virtual修饰符的指针会调用具体指向的类作用域中的方法。而未使用的会直接使用指针类作用域中的方法。
什么时候推荐使用virtual修饰符:

  • 析构函数(防止内存泄漏)
  • 派生类中重写了基类函数的函数(有点绕)。

17.c++中的容器类型

deque,list,queue,priority_queue,stack,
vector,map,multimap,set,multiset,bitset,forward_list,
unordered_map,unordered_multimap,unordered_multiset.

18.copy构造跟赋值构造函数区分

未被定义的使用copy构造,已被定义使用赋值,示例

class B
{
private:
    /* data */
public:
    B(/* args */);
    ~B();
    B(B& b);//Copy构造
    B& operator=(B& b);//copy赋值
};

int main()
{   
    B a;
    B b;
    B c = a;//Copy构造
    b = a ;//Copy赋值
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫的于

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值