shape.h
#pragma once
#ifndef __SHAPE_H__
#define __SHAPE_H__
struct Point
{
double x;
double y;
};
struct CircleA
{
double x;
double y;
double r;
};
struct CircleB:public Point
{
double r;
};
struct CircleC
{
Point p;
double r;
};
#endif
test1.cpp
#include <iostream>
using namespace std;
#include "shape.h"
int main()
{
Point* p = new Point();
p->x = 1;
p->y = 2;
double* dp = (double*)p;
cout << dp[0] << " " << dp[1] << endl;
double* dpp = &p->x;
cout << dpp[0] << " " << dpp[1] << endl;
delete p;
CircleA* c1 = new CircleA();
c1->x = 3;
c1->y = 4;
c1->r = 5;
p = (Point*)c1;
cout << "circle as point " << p->x << " " << p->y << endl;
CircleB* c2 = (CircleB*)c1;
cout << "circleA as circleB " << c2->x << " " << c2->y << " " << c2->r << endl;
double* dppp = &c2->x;
cout << dppp[0] << " " << dppp[1] << " "<< dppp[2] << endl;
cout << sizeof(CircleA) << " " << sizeof(CircleB) << " " << sizeof(CircleC) << endl;
CircleC* c3 = new CircleC();
Point* p3 = &c3->p;
CircleC* c33 = (CircleC*)p3;
return 0;
}