C++ 之const详解
发布日期:2021-05-14 16:35:30 浏览次数:22 分类:精选文章

本文共 4074 字,大约阅读时间需要 13 分钟。

C������������const������

���C������������const���������������������������������������������������������������������������������������������������������������������const������������������������������������

������const������������������

const���������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������const������

    • ������*������������������������������������������������������������������������������������������������������������������������������������������������������������
    • ������*������������������������������������������������������������������������������������������������������������������
  • ���������const������������������������*������������������������������������������������������������������

������������������������

void ConstDemo1() {
int num1 = 1024;
const int num2 = num1;
// num2 = 2048; // ������������
cout << num2 << endl;
}

������const������������������

������������������const���������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������

void ConstDemo2(int num) {
// num = 123; // ������������������
}

const������������

���const������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������

class Computer {
public:
Computer(int core) {
m_core = core;
}
void buy() {}
void buy(int core) {}
void SetCore(int core) {
m_core = core;
}
int GetCore() const {
return m_core;
}
private:
int m_core; // CPU ������
};
void ConstDemo3(const Computer &computer) {
// ���������������const���������������comppeter.buy(123) ���������
}

������const���������������

const������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������������������������

���������

class Computer {
public:
Computer(int core) {
m_core = core;
}
void buy() {}
void buy(int core) {}
void SetCore(int core) {
m_core = core;
}
int GetCore() const {
return m_core;
}
private:
int m_core;
};
const Computer &GetMax(const Computer &com1, const Computer &com2) {
if (com1.GetCore() > com2.GetCore()) {
return com1;
}
return com2;
}

������������������������������������

void ReturnLocalObject() {
struct A {
int x;
A(int a) : x(a) {}
~A() {}
};
return A(123);
}

������const������������

������������������const������������������������������������������������������������������������������������������������������������������������functi


������

  • ConstDemo.cpp
#ifndef CONSTDEMO_H_INCLUDED
#define CONSTDEMO_H_INCLUDED
#include
using namespace std;
// 1. const������������������
void ConstDemo1() {
int num1 = 1024;
const int num2 = num1;
// ������������ num2 ������
cout << num2 << endl;
}
// 2. const������������������
void ConstDemo2(int num) {
// ������������ num ������
}
// ��� Computer
class Computer {
public:
Computer(int core) {
m_core = core;
}
void buy() {}
void buy(int core) {}
void SetCore(int core) {
m_core = core;
}
int GetCore() const {
return m_core;
}
private:
int m_core; // CPU ������
};
// 3. const���������������
const Computer &GetMax(const Computer &com1, const Computer &com2) {
if (com1.GetCore() > com2.GetCore()) {
return com1;
}
return com2;
}
// 4. const������������
void ModifyValue() const {
// ������������ m_core ������
}
#endif
  • main.cpp
#include 
#include "ConstDemo.h"
int main() {
ConstDemo1();
return 0;
}
上一篇:C++ 之友元函数
下一篇:C++ 之运算符重载

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月30日 06时07分02秒