C++ constant member constant return value details
- 2020-05-24 05:51:45
- OfStack
Conclusion:
1. Constant data member, form: const Type m_tData;
1) the constant data member needs to be given in the constructor list. The constructor can be assigned with constant value, and it can also be assigned when it is instantiated.
2) no value can be assigned in the assignment function, which plays the role of protecting constant data members, contrary to the role of friends.
2. Constant member function, form: type funname(type1 arg1,type2 arg2,...) const
1) constant member function, cannot modify class data member, cannot call nonconstant function.
2) the function of constant member functions can be effectively divided into functions that can be modified and functions that cannot be modified; You should be good at using constant member functions in the future.
3. Return a constant function, which can be a constant pointer, pointer constant, constant, form:
const type* funcname(type1 arg1,type2 arg2, ..)
type* const funcname(type1 arg1,type2 arg2, ..)
const funcname(type1 arg1,type2 arg2, ..)
Their return type is not important for use, but the type of object assigned determines what can be done later.
Both constant Pointers and pointer constants can be assigned to constant pointer objects, which can perform p++ operations, not *p operations.
Both constant Pointers and pointer constants can be assigned to pointer constants, but pointer constants can only do *p operations, not p++ operations.
Common types of functions that return constants, in order to prevent member functions from performing operations between return values, to prevent ugly code,
The return value is a constant function, indicating that this value within the class is not easily changed by external users, which can make the declaration of the class more appropriate and easier to understand.
#include "stdafx.h"
#include <iostream>
using namespace std;
class CTest
{
public:
CTest(int nid, int nlimit):m_cntLimit(nlimit)
{
//m_cntLimit = nlimit;// Constant members must be given in the constructor list
m_nId = nid;
}
~CTest(){};
int GetID() const
{
//m_nId++; Constant member functions cannot modify objects
//ClientGetObj(); Constant member functions cannot call nonconstant member functions
return m_nId;
}
CTest operator =(const CTest &b)
{
this->m_nId = b.m_nId;
//this->m_cntLimit = b.m_cntLimit;// Constant data members cannot be copied
return (*this);
}
int ClientGetID()
{
return GetID();
}
CTest* const GetObj()
{
return this;
}
CTest* ClientGetObj()
{
return this;
}
const int GetID()
{
return m_nId;
}
void Print()
{
cout<<"m_nId:"<<m_nId<<", const m_cntLimit"<<m_cntLimit<<endl;
}
void PrintCnt() const
{
cout<<"m_nId:"<<m_nId<<", const m_cntLimit"<<m_cntLimit<<endl;
}
private:
int m_nId;
const int m_cntLimit;
};
void main()
{
CTest Obj1(1, 1000);
CTest Obj2(2, 2000);
CTest* pObj = Obj1.ClientGetObj();
pObj->Print();
CTest objTemp = *(Obj1.ClientGetObj());
*pObj = *(Obj2.ClientGetObj());
pObj->Print();
// reset
*pObj = objTemp;
cout<<"-------------const display---------------"<<endl;
/*const */CTest* const pCntObj = Obj1.GetObj();// Both constant Pointers and pointer constants can be assigned to constant Pointers
pCntObj->PrintCnt();
*pCntObj = *(Obj2.GetObj());
pCntObj->PrintCnt();
/*const */int nid = pCntObj->GetID();// A constant return value can be assigned to a variable
nid++;
cout<<"new nid is:"<<nid<<endl;
//*pCntObj = *(Obj1.GetObj());// Constant pointer object, cannot proceed *p Operation, ok p++ operation
while(1);
}