Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created February 16, 2014 17:59
Show Gist options
  • Select an option

  • Save kkdai/9038143 to your computer and use it in GitHub Desktop.

Select an option

Save kkdai/9038143 to your computer and use it in GitHub Desktop.
#include <iostream>
class AA
{
public:
static int i;
//static int i = 0; // compile error
static const int KKKK= 10;//OKAY
int k = 0; //warning, just allow after c++/11 to initialize value in class
void fool(){printf("the value of i is %d\n", i);}//static function only can use "static const", can't use "static" variables
};
class BB:public AA
{
public:
static int i;
};
int AA::i = 5;
//the only way to initialize a "static" member variable.
//For "static const" member variable both to initialize here or in class is okay
int main(int argc, const char * argv[])
{
AA::i=4;
printf("Size of AA is %d\n", sizeof(AA));// only 4, won't count static member;
AA* p = NULL;//new AA;
AA* p2 = new AA;//new AA;
p2->i=3;
AA* q = NULL;//new AA;
p->fool();
//以下三種資料均為同一個.. 要注意
printf("value of of AA::i=%d, address of AA::i=%d\n", AA::i, &AA::i);
printf("value of of p->i=%d, address of p->i=%d\n", p->i, &p->i);
printf("value of of q->i=%d, address of q->i=%d\n", q->i, &q->i);
printf("value of of p2->i=%d, address of p2->i=%d\n", p2->i, &p2->i);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment