将班级成员存储为原始指针的替代方案
在下面显示的代码示例中 - 在container
类中,拥有(并且负责销毁)两个对象c,d
,是子类抽象类b
的。 容器
对象可以创建新的ObjectDisplay
在其构造函数中采用一种B。我可以将抽象类型B作为指针将其传递到objectDisplay
中,并将其存储为原始指针。但这并不理想地存储&使用原始指针,并始终检查是否是空指针。如果B不是抽象类,我可以将其传递给objectDisplay
作为参考(即objectdisplay(b& b)
)。但是,由于我无法更改B,我想知道将B*对象存储为objectDisplay
中的原始指针的态度是什么?
// B is abstract
class B
{
public:
virtual int getDefault() = 0;
};
class C : public B
{
public:
int getDefault() override { return 1; }
};
class D : public B
{
public:
int getDefault() override { return 5; }
};
class ObjectDisplay
{
public:
ObjectDisplay (B* b) : object (b) {}
void someFunction()
{
const auto result = b->getDefault();
// do something
}
private:
B* object;
};
class Container
{
public:
void addDisplay()
{
displays.push_back (ObjectDisplay (&c));
displays.push_back (ObjectDisplay (&d));
}
private:
C c;
D d;
std::vector<ObjectDisplay> displays;
};
In the code example shown below - in Container
class, it owns (and is responsible fore destroying) two objects c, d
, which are subclasses of an abstract class B
. Container
object can create new ObjectDisplay
that takes a kind of B in its constructor. I can pass the abstract type B as a pointer into ObjectDisplay
and store it as a RAW pointer. But it's not ideal to store & use a raw pointer and always check if it's a null pointer. If B wasn't an abstract class, I could pass it in ObjectDisplay
as a reference (ie. ObjectDisplay (B& b)
). But since I can't change B, I wonder what's the aternative of storing B* object as a raw pointer in ObjectDisplay
?
// B is abstract
class B
{
public:
virtual int getDefault() = 0;
};
class C : public B
{
public:
int getDefault() override { return 1; }
};
class D : public B
{
public:
int getDefault() override { return 5; }
};
class ObjectDisplay
{
public:
ObjectDisplay (B* b) : object (b) {}
void someFunction()
{
const auto result = b->getDefault();
// do something
}
private:
B* object;
};
class Container
{
public:
void addDisplay()
{
displays.push_back (ObjectDisplay (&c));
displays.push_back (ObjectDisplay (&d));
}
private:
C c;
D d;
std::vector<ObjectDisplay> displays;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
,如果
B
是抽象类,则仍然可以通过参考将其传递。b&amp;对象
可以绑定到b
的子类的实例。它的行为与指针几乎相同。如 cppref :
在
b&amp;
中声明objectdisplay
中的成员,然后通过参考来构造它。demo
请参阅在线
由于您正在传递临时
objectDisplay
直接在push_back
中构造的对象,因此我建议您使用emplace_back
。No, if
B
is an abstract class, you can still pass it by reference.B& object
can be bound to an instance ofB
's subclass. It behaves almost the same as pointers.As quoted in cppref:
Declare a member of
B&
inObjectDisplay
and construct it through a reference.See online demo
Aside:
Since you are passing a temporary
ObjectDisplay
object directly constructed inpush_back
, I recommend you to useemplace_back
.仅仅因为
b
是一个抽象类,并不意味着您必须将其传递给它并将其存储为 pointer 。您也可以将其传递给它,并将其存储为参考。多态性与指针和参考作用。并且使用参考确实可以解决您的nullptr
问题,例如:演示
在线 因为
c
和d
在objectdisplay
在显示
中的对象中,您都会很好,无论您使用指针还是参考。Just because
B
is an abstract class does not mean you are required to pass it around and store it as a pointer. You CAN pass it around and store it as a reference as well. Polymorphism works with pointers AND references. And using a reference would indeed solve yournullptr
issue, eg:Online Demo
As long as
c
andd
outlive theObjectDisplay
objects indisplays
, you will be just fine, whether you use pointers or references.