C++ 将基础 class 成员链接到派生 class 成员
C++ linking base class members to derived class members
class B_mem {
public:
int b_var;
};
class D_mem : public B_mem {
public:
int d_var;
};
class B {
public:
B_mem b_member;
};
class D : public B {
public:
D_mem d_member;
};
int main () {
D derived;
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
我如何构建 classes 以便当我 set/update D_mem 时,它会自动 sets/updates B_mem(如果相关)?在上面的示例中,我创建了 D 并填充 D_mem,但随后使用类型 B 的指针访问 D。我希望能够通过以下方式访问 D 中 D_mem 的基础 class 成员B_mem.
我想知道是否有一些具有多态性、复制构造函数或集合函数的东西可以让我做到这一点而不必手动保持 D_mem 和 B_mem 一致。
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
当然不是。
线条
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
没有改变derived
的成员变量。它们仍处于未初始化状态。
您可以使用:
int main () {
D derived;
D_mem& dmem = derived.d_member; // Get a reference to an existing object
dmem.b_var = 2; // Modify the referenced object
dmem.d_var = 3;
// That still doesn't change b_member.
// Need to update it too.
derived.b_member.b_var = 2;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
或
int main () {
D derived;
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
derived.d_member = dmem; // Set the value of derived.
derived.b_member = dmem;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
回复:
I am wondering if there is something with polymorphism, copy constructors, or set functions that will allow me to do this without having to manually keep D_mem
and B_mem
in agreement.
如果您提供处理这些细节的成员函数并将成员变量设为私有,那么您可以做到这一点,但它会变得混乱,因为您在 D
中基本上有两个 B_mem
实例。
如果使用指针而不是对象,代码会变得更简单、更易于维护。
这是一个示例实现:
#include <iostream>
#include <memory>
class B_mem {
public:
int b_var;
virtual ~B_mem() {}
};
class D_mem : public B_mem {
public:
int d_var;
};
class B {
protected:
std::shared_ptr<B_mem> b_member;
public:
B(std::shared_ptr<B_mem> member) : b_member(member){}
virtual ~B() {}
virtual B_mem& getMember() = 0;
virtual B_mem const& getMember() const = 0;
};
class D : public B {
public:
D() : B(std::shared_ptr<B_mem>(new D_mem)){}
D_mem& getMember()
{
return *(std::dynamic_pointer_cast<D_mem>(b_member));
}
D_mem const& getMember() const
{
return *(std::dynamic_pointer_cast<D_mem>(b_member));
}
};
int main () {
D derived;
derived.getMember().b_var = 2;
derived.getMember().d_var = 3;
B* b_ptr = &derived;
std::cout << b_ptr->getMember().b_var << std::endl;
}
输出:
2
class B_mem {
public:
int b_var;
};
class D_mem : public B_mem {
public:
int d_var;
};
class B {
public:
B_mem b_member;
};
class D : public B {
public:
D_mem d_member;
};
int main () {
D derived;
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
我如何构建 classes 以便当我 set/update D_mem 时,它会自动 sets/updates B_mem(如果相关)?在上面的示例中,我创建了 D 并填充 D_mem,但随后使用类型 B 的指针访问 D。我希望能够通过以下方式访问 D 中 D_mem 的基础 class 成员B_mem.
我想知道是否有一些具有多态性、复制构造函数或集合函数的东西可以让我做到这一点而不必手动保持 D_mem 和 B_mem 一致。
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
当然不是。
线条
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
没有改变derived
的成员变量。它们仍处于未初始化状态。
您可以使用:
int main () {
D derived;
D_mem& dmem = derived.d_member; // Get a reference to an existing object
dmem.b_var = 2; // Modify the referenced object
dmem.d_var = 3;
// That still doesn't change b_member.
// Need to update it too.
derived.b_member.b_var = 2;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
或
int main () {
D derived;
D_mem dmem;
dmem.b_var = 2;
dmem.d_var = 3;
derived.d_member = dmem; // Set the value of derived.
derived.b_member = dmem;
B* b_ptr = &derived;
std::cout << b_ptr->b_member.b_var; // Doesn't print 2
}
回复:
I am wondering if there is something with polymorphism, copy constructors, or set functions that will allow me to do this without having to manually keep
D_mem
andB_mem
in agreement.
如果您提供处理这些细节的成员函数并将成员变量设为私有,那么您可以做到这一点,但它会变得混乱,因为您在 D
中基本上有两个 B_mem
实例。
如果使用指针而不是对象,代码会变得更简单、更易于维护。
这是一个示例实现:
#include <iostream>
#include <memory>
class B_mem {
public:
int b_var;
virtual ~B_mem() {}
};
class D_mem : public B_mem {
public:
int d_var;
};
class B {
protected:
std::shared_ptr<B_mem> b_member;
public:
B(std::shared_ptr<B_mem> member) : b_member(member){}
virtual ~B() {}
virtual B_mem& getMember() = 0;
virtual B_mem const& getMember() const = 0;
};
class D : public B {
public:
D() : B(std::shared_ptr<B_mem>(new D_mem)){}
D_mem& getMember()
{
return *(std::dynamic_pointer_cast<D_mem>(b_member));
}
D_mem const& getMember() const
{
return *(std::dynamic_pointer_cast<D_mem>(b_member));
}
};
int main () {
D derived;
derived.getMember().b_var = 2;
derived.getMember().d_var = 3;
B* b_ptr = &derived;
std::cout << b_ptr->getMember().b_var << std::endl;
}
输出:
2