Here is the test of const member functions
#include <iostream>
using namespace std;
template <typename T>
class Data
{
public:
Data();
Data(const Data<T> &d);
void setData(T const &d);
const T getData() const;
const T getData0();
const T getData1() const;
private:
T myData;
};
template <typename T>
Data<T>:
ata() {}
template <typename T>
Data<T>:
ata(const Data<T> &d) { myData = d.getData(); }
template <typename T>
void Data<T>::setData(T const &d) { myData = d; }
template <typename T>
const T Data<T>::getData() const
{
return myData;
}
template <typename T>
const T Data<T>::getData0()
{
myData = 3;
return myData;
}
template <typename T>
const T Data<T>::getData1() const
{
myData = 3;
return myData;
}
int main()
{
Data<double> theData;
theData.setData(5);
cout<<theData.getData()<<endl; // output: 5
cout<<theData.getData0()<<endl; // output: 3
// cout<<theData.getData1()<<endl; // can't get compiled.
Data<double> newData(theData);
cout<<theData.getData()<<endl; // output: 3
cout<<newData.getData0()<<endl; // output: 3
// cout<<newData.getData1()<<endl; // can't get compiled.
return 0;
}
The const after the mem function do restricted the access to mem variable.
My new question is, it seems that I can just ignore the const before the mem functions.
like, I can change 'const T getData() const; ' to 'T getData() const; '.