
本文共 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#includeusing namespace std;// 1. const������������������void ConstDemo1() { int num1 = 1024; const int num2 = num1; // ������������ num2 ������ cout << num2 << endl;}// 2. const������������������void ConstDemo2(int num) { // ������������ num ������}// ��� Computerclass 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;}
发表评论
最新留言
关于作者
