...
const Str cs(8);
Str& rs = cs; //error, rs can't refer to cs.
...
Because the "cs" is a const Str instance, a Str reference is not allowed to refer to it.
...
const Str cs(8);
const Str& rs = cs; //add const, ok.
...
Yes, a const Str reference can refer to a const Str instance.
By the way, there another way to slove the above error problem.
...
Str cs(8); // removed const
Str& rs = cs; //ok
...
A non-const Str reference can refer to a non-const Str instance.
What is more!
class C{
public:
...
void fun() const {};
...
private:
...
}
A const function is only a member of a class. The const function is meant that it should
be used for a constants or a non-constants. A non-const function only can be used for a non-constant.
For example.
const C cc;
cc.fun(); //ok
const C nc;
nc.fun(); //error.
More example.
#include <iostream>
using namespace std;
class Str{
public:
Str(){
data = 0;
len = 0;
}
Str(int d){
data = d;
len = d * 2;
}
int getData() const{
return data;
}
int getData(){
cout<<"non-const getData()"<<endl;
return data;
}
int length() const{
return len;
}
int length(){
cout<<"non-const length()"<<endl;
return len;
}
int add(const Str& s) const{
int ret = data * s.getData();
/*Str *sp = const_cast<Str*>(&s);
sp->clear();*/
return ret;
}
private:
mutable int data;
mutable int len;
friend void clear(const Str& s);
};
void clear(const Str& s) {
s.data = 0;
s.len = 0;
}
int main(){
Str s(5);
//cout<<s.length()<<" "<<s.getData()<<endl;
const Str cs(8);
cout<<cs.length()<<" "<<cs.getData()<<endl;
cout<<s.add(cs)<<endl;
clear(cs);
cout<<cs.length()<<" "<<cs.getData()<<endl;
Str& rs = cs;
return 0;
}
const Str cs(8);
Str& rs = cs; //error, rs can't refer to cs.
...
Because the "cs" is a const Str instance, a Str reference is not allowed to refer to it.
...
const Str cs(8);
const Str& rs = cs; //add const, ok.
...
Yes, a const Str reference can refer to a const Str instance.
By the way, there another way to slove the above error problem.
...
Str cs(8); // removed const
Str& rs = cs; //ok
...
A non-const Str reference can refer to a non-const Str instance.
What is more!
class C{
public:
...
void fun() const {};
...
private:
...
}
A const function is only a member of a class. The const function is meant that it should
be used for a constants or a non-constants. A non-const function only can be used for a non-constant.
For example.
const C cc;
cc.fun(); //ok
const C nc;
nc.fun(); //error.
More example.
#include <iostream>
using namespace std;
class Str{
public:
Str(){
data = 0;
len = 0;
}
Str(int d){
data = d;
len = d * 2;
}
int getData() const{
return data;
}
int getData(){
cout<<"non-const getData()"<<endl;
return data;
}
int length() const{
return len;
}
int length(){
cout<<"non-const length()"<<endl;
return len;
}
int add(const Str& s) const{
int ret = data * s.getData();
/*Str *sp = const_cast<Str*>(&s);
sp->clear();*/
return ret;
}
private:
mutable int data;
mutable int len;
friend void clear(const Str& s);
};
void clear(const Str& s) {
s.data = 0;
s.len = 0;
}
int main(){
Str s(5);
//cout<<s.length()<<" "<<s.getData()<<endl;
const Str cs(8);
cout<<cs.length()<<" "<<cs.getData()<<endl;
cout<<s.add(cs)<<endl;
clear(cs);
cout<<cs.length()<<" "<<cs.getData()<<endl;
Str& rs = cs;
return 0;
}