C/C++におけるconstの問題
コードセグメントとコメントを直接貼り付けると、一目瞭然です.//fはこの文が間違っていて実行できないことを示しています.//tは正しいです.
#include
#include "stdio.h"
int main() {
int a=101;
int b = 102;
const int c = 100;
int *p;
const int* p1 = &a; // const int
int* const p2 = &a; // int cnost ( )
int const *p3 = &a;// const int (int const = const int, , )
const int *p4 = &a; // const int
*p1 = 100; //f ( 1:) const int , , 。
a=100; //t a ? a const int , const int int ( , )
// *p1 , a 。
*p2 = 100; //t
*p3 = 100; //f
*p4 = 100; //f
p1 = &b;//t
p2 = &b;//f
p3 = &b;//t
p4 = &b;//t
p = p1;//f const int int
p = &c;//f const int int
p = (int *)&c;//t
printf("
%d",*p1);
printf("
%d",*p2);
printf("
%d",*p3);
printf("
%d",*p4);
return 0;
}
#include
#include "stdio.h"
//const
// :const 。
// , , ( ),
//Q:const , const 。
//c/c++ const ( )
//
//A: : ?
// , const , 。
//
// void XXX() const ; const ( )
//Q: const , ?
//A: c++ mutable , const
class const_test
{
private:
int a ;
mutable int b ;
public:
void show() const{
printf("%d",a);
printf("
");
printf("%d",b);
printf("
end
");
}
void show_nc() {
printf("%d",a);
}
void set_b() const{
b = 50;
}
const_test(int d_a, int d_b):a(d_a),b(d_b){};
~const_test(){};
};
int main() {
const_test const t1(100,100);
t1.show();
// t1.show_nc();
t1.set_b();
t1.show();
//
const_test t2(100,100);
const_test &t3 = t2;
t2.show();
t2.set_b();
t3.show();
}
//
//1。 const
//2.
//3. ( )