标签:
const int i = 10;
int *pi = (int *)&i;
*pi = 30;
cout<<i<<endl;
int f(){return 10}
const int i = f();
int *pi = (int*)&i;
*pi = 30;
cout<<i<<endl;
//file_1.cc
int counter;
//file_2.cc
extern int counter;
counter++;
//file_1.cc//defines and initializes a const that is accessible to other files
extern const int i =f();
//file_2.cc//uses i from file_1.cc
extern const int i;
char s[] = "Gorm";
const char* pc = s; //a pointer to const charpc[3] = ‘g‘; //errorpc = p; //okchar * const cp = s ; //a cosnt pointer to charcp[3] = ‘a‘; //okcp = p; //errorconst char * const cpc =s ;//a const pointer to const char
cpc[3] = ‘a‘; //errorcpc = p; //error
clss Stack
{
public:
void Push(int elem);
int Pop(void);
int GetCount(void) const;
private:
int m_num;
int m_data[100];
};
int Stack::GetCount(void) const
{
++m_num;//error,本函数为const
pop();//error,调用非const函数
return m_num;
}
const int fingers = 10; //same as static const int fingers = 10;
int main(void)
{
...
const int fingers =10;
const char * warning = "Wak!";
标签:
原文地址:http://www.cnblogs.com/codetravel/p/4534481.html