我一直在尝试创建一个类层次结构,让每个类都包含一个包含数据的嵌套结构。
////////Class.h////////////////////////////
#ifndef _CLASS_H_
#define _CLASS_H_
#include <stdio.h>
class A{//Base
protected:
struct dataA{
int v1, v2;
};
public:
A();
virtual void met1();
dataA * const set;
const dataA * const get;
};
class B : public A{//Child1
protected:
struct dataB : public dataA{
int v3, v4;
};
public:
B();
virtual void met2();
dataB * const set;
const dataB * const get;
};
class C : public B{//Child2
struct dataC : public dataB{
int v5, v6;
};
public:
C();
void met3();
dataC * const set;
const dataC * const get;
};
#endif
////////Class.cpp///////////////////////////
#include "Class.h"
A::A() : set( new dataA ), get( set ) {
this->set->v1 = -1;
this->set->v2 = -1;
}
void A::met1() {
printf( "%i, %i", this->get->v1, this->get->v2 );//PRINTS "-1, -1"
}
B::B() : A(), set( new dataB ), get( set ) {
this->set->v3 = -1;
this->set->v4 = -1;
}
void B::met2() {
printf( "%i, %i", this->get->v1, this->get->v2 );//An attempt to access C's "get" variable, PRINTS "-1, -1"
}
C::C() : B(), set( new dataC ), get( set ) {
this->set->v5 = -1;
this->set->v6 = -1;
}
void C::met3() {
printf( "%i, %i", this->get->v1, this->get->v2 );//PRINTS "2, 3"
}
///////main.cpp////////////////////////////////
#include "Class.h"
int main() {
C memb;
memb.set->v1 = 2;
memb.set->v2 = 3;
printf( "%i, %i", memb.get->v1, memb.get->v2 );//PRINTS "2, 3"
memb.met2();//PRINTS "-1, -1"
return 0;
}
所以,我一直在尝试做的是在声明一个C类型的成员后,从B访问一个可以访问C的数据结构的继承函数。 void指针不能完成这项工作,因为它无法指向对象类型。我尝试将C的地址传递给B,但它们都是不同的类型。首先,如果我从主块访问v1,只要我为它设置了一个值,如示例所示,我得到一个正确的值 - 我设置的值。但是,如果我尝试访问同一个变量,而是从B类访问它,它会打印该值,就像之前没有设置的那样,而不是构造函数。所以我试图创建一个从B类到C类数据结构的链接 - 到C类的数据父类B的数据结构。示例代码尚未编译。 此致!
答案 0 :(得分:2)
我很确定这很接近你想要的,但我必须告诉你我需要在写完之后淋浴。我不会做很多事情,但是......
#include <cstdio>
#include <cstdlib>
using namespace std;
class A{//Base
protected:
struct dataA
{
int v1, v2;
} * const data;
A(dataA* const p)
: data(p)
{
p->v1=-1;
p->v2=-1;
};
public:
virtual void met1()
{
printf( "%i, %i", get()->v1, get()->v2 );
}
const dataA * const get() const { return data; }
dataA * const set() const { return data; }
};
class B : public A {//Child1
protected:
struct dataB : public dataA
{
int v3, v4;
} * const data;
// protected constructor
B(dataB * const p)
: A(p), data(p)
{
p->v3 = 0;
p->v4 = 0;
};
public:
virtual void met2()
{
printf( "%i, %i, %i, %i", get()->v1, get()->v2, get()->v3, get()->v4);
}
const dataB * const get() const { return data; }
dataB * const set() const { return data; }
};
class C : public B
{
struct dataC : public dataB
{
int v5, v6;
} * const data;
public:
C() : B(new dataC),
data(static_cast<dataC*>(B::data))
{
data->v5 = 0;
data->v6 = 0;
};
~C()
{
// note, the pointers in B and A are left
// dangling after this is done.
delete data;
}
virtual void met2()
{
printf( "%i, %i, %i, %i, %i, %i",
get()->v1, get()->v2,
get()->v3, get()->v4,
get()->v5, get()->v6);
}
const dataC * const get() const { return data; }
dataC * const set() const { return data; }
};
///////main.cpp////////////////////////////////
int main() {
C memb;
memb.set()->v1 = 2;
memb.set()->v2 = 3;
printf( "%i, %i\n", memb.get()->v1, memb.get()->v2 );
memb.met2();
return 0;
}
<强>输出强>
2, 3
2, 3, 0, 0, 0, 0
<强>更新强>
OP希望在没有使用成员变量的get()和set()成员的情况下看到这一点。再次,只是丑陋,但在这里你去:
#include <cstdio>
#include <cstddef>
class A{//Base
protected:
struct dataA
{
int v1, v2;
};
dataA * const set;
const dataA * const get;
A(dataA* const p)
: get(p), set(p)
{
p->v1=-1;
p->v2=-1;
};
public:
virtual void met1()
{
printf( "%i, %i\n", get->v1, get->v2 );
}
};
class B : public A {//Child1
protected:
struct dataB : public dataA
{
int v3, v4;
};
dataB * const set;
const dataB *const get;
// protected constructor
B(dataB * const p)
: A(p), set(p), get(p)
{
p->v3 = 0;
p->v4 = 0;
};
public:
virtual void met2()
{
printf( "%i, %i, %i, %i\n", get->v1, get->v2, get->v3, get->v4);
}
};
class C : public B
{
struct dataC : public dataB
{
int v5, v6;
};
public:
dataC * const set;
const dataC * const get;
C() : B(new dataC),
set(static_cast<dataC *const>(B::set)),
get(static_cast<const dataC *const>(B::get))
{
set->v5 = 0;
set->v6 = 0;
}
~C()
{
// note, the pointers in B and A are left
// dangling after this is done.
delete set;
}
virtual void met2()
{
printf( "%i, %i, %i, %i, %i, %i\n",
get->v1, get->v2,
get->v3, get->v4,
get->v5, get->v6);
}
};
///////main.cpp////////////////////////////////
int main() {
C memb;
memb.set->v1 = 2;
memb.set->v2 = 3;
printf( "%i, %i\n", memb.get->v1, memb.get->v2 );
memb.met2();
B& obj = memb;
obj.met1();
obj.met2();
return 0;
}
<强>输出强>
2, 3
2, 3, 0, 0, 0, 0
2, 3
2, 3, 0, 0, 0, 0