没想到会遇到这样的问题,
在VC6环境下测试,重点请看红色字体与图片。
#include "stdafx.h"
#include <stdio.h>
typedef struct sData
{
static const char* text;
static int val_a;
static int val_b;
} sData, *Self_Ptr;
typedef struct
{
char* text;
int val_a;
int val_b;
}* Other_Ptr;
const char* sData::text = "this is a test string\0";
int sData::val_b = 200;
int sData::val_a = 100;
int main(int argc, char* argv[])
{
Self_Ptr p_self = (Self_Ptr)&(sData::text);
Other_Ptr p_other = (Other_Ptr)&(sData::text);
printf("%d\n", sizeof(sData)); //----1, static成员不计入sizeof
printf("val_a: %d-%d\n", p_self->val_a, sData::val_a); //----val_a: 100-100
printf("val_b: %d-%d\n", p_self->val_b, sData::val_b); //----val_b: 200-200
printf("val_a: %d-%d\n", p_self->val_a, p_other->val_a);//----val_a: 100-200
printf("val_b: %d-%d\n", p_self->val_b, p_other->val_b);//----val_b: 200-100
return 0;
}