[英]C++: inheritance or class member for C struct?
I have a plain old C struct
: 我有一个普通的旧C struct
:
struct eggplant {
double *ptr1;
double *ptr2;
... // many more
}
and a function that manages the allocation the memory pointed to by the pointers, and returns a new eggplant
instance: 和一个管理指针指向的内存分配的函数,并返回一个新的eggplant
实例:
struct eggplant *create_eggplant(int n);
The above function allocates a chunk of memory (including space for the newly created struct) and distributes it to the pointers. 上面的函数分配一块内存(包括新创建的struct的空间)并将其分配给指针。
I want to extend struct eggplant
in C++11. 我想在C ++ 11中扩展struct eggplant
。 I could do that by keeping a pointer to the struct as a 我可以通过保持指向结构的指针来做到这一点
class EggPlant {
...
private :
struct eggplant *plant;
}
or I could try via 或者我可以尝试通过
class EggPlant : private struct eggplant {
...
}
The first option allows me to use the create_eggplant
function. 第一个选项允许我使用create_eggplant
函数。 However, the second option looks more straightforward from a conceptual point of view (the class EggPlant is an eggplant, it doesn't have one). 然而,第二个选择看起来从概念的角度来看更直接(类茄子是茄子,它不具有一个)。
I tried 我试过了
this = create_eggplant(...);
in the constructor but that does not work for the obvious reason that you cannot overwrite a pointer to a class that you are constructing ( lvalue required
). 在构造函数中,但由于您无法覆盖指向正在构造的类的指针( lvalue required
),这不起作用。
Can I inherit the struct
but still use my create_eggplant
function in some useful way? 我可以继承struct
但仍然以一些有用的方式使用我的create_eggplant
函数吗? Or, is it anyway better to keep a pointer to the struct? 或者,最好保留指向结构的指针?
Your choice to have struct eggplant *create_eggplant(int n);
您选择使用struct eggplant *create_eggplant(int n);
manage its own memory is in conflict with C++ inheritance. 管理自己的内存与C ++继承冲突。 Inheritance also implies management of the location of the base object. 继承还意味着管理基础对象的位置。
If you changed your C function to return a copy: 如果您更改了C函数以返回副本:
struct eggplant create_eggplant(int n);
You could also inherit from that class: 您也可以从该类继承:
class EggPlant : private eggplant {
EggPlant(int n) : eggplant{ create_eggplant(n) }
{
}
...
};
No, you would need a new function if you wish to inherit. 不,如果你想继承,你需要一个新的功能。 If you must use the existing function a member is the best you will be able to manage. 如果您必须使用现有功能,则会员是您能够管理的最佳成员。
struct eggplant *create_eggplant(int n);
Should be just: 应该只是:
eggplant *create_eggplant(int n);
This isn't C, you don't have to type struct
everywhere. 这不是C,你不必在任何地方输入struct
。
However, the second option looks more straightforward from a conceptual point of view (the class
EggPlant
is an eggplant, it doesn't have one). 然而,从概念的角度来看,第二种选择看起来更直接(EggPlant
类是一种茄子,它没有一种)。
That's wrong. 那是错的。 Only public inheritance models is-a . 只有公共继承模型是 -a。 Private inheritance models has-a or is-implemented-in-terms-of . 私有继承模型具有 -a或is-implemented-terms-of 。
Can I inherit the struct but still use my create_eggplant function in some useful way? 我可以继承结构但仍然以一些有用的方式使用我的create_eggplant函数吗?
No, not in a useful way, especially as your description of create_eggplant
sounds like malloc
is used in it. 不,不是有用的方式,特别是因为你的create_eggplant
声音的描述就像使用了malloc
。
Or, is it anyway better to keep a pointer to the struct? 或者,最好保留指向结构的指针?
No, pointers make code more complicated. 不,指针使代码更复杂。
If you want to use private inheritance, then either add a constructor to eggplant
that more or less does what create_eggplant
does now, except of allocating memory for the eggplant
itself, or modify create_eggplant
to become something like void init_eggplant(eggplant& e)
, then do this in the constructor of EggPlant
: 如果你想使用私有继承,那么要么向eggplant
添加一个或多或少做create_eggplant
现在做的构造函数,除了为eggplant
本身分配内存,或者修改create_eggplant
变成类似void init_eggplant(eggplant& e)
,然后做这在EggPlant
的构造函数中:
EggPlant::EggPlant() :
eggplant()
{
init_eggplant(*this);
}
Or just don't use private inheritance but a normal data member: 或者只是不使用私有继承,而是使用普通数据成员:
class EggPlant
{
// ...
private:
eggplant my_eggplant;
}
As you can see, the common goal of all solutions is to get rid of the dynamic memory allocation for eggplant
. 如您所见,所有解决方案的共同目标是摆脱eggplant
的动态内存分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.