使用现有基类对象创建派生类对象?

发布于 2024-10-25 16:03:29 字数 130 浏览 2 评论 0原文

是否有可能(或快速解决方法)创建定义为派生类的对象,而无需在内存中创建基类对象;相反,派生对象应该引用基类实际存在的对象(“接管”其内存驻留)?出于速度原因,这是需要的 - 创建新的派生对象,将数据从基类对象复制到它,然后销毁基对象需要太多时间。

Is there a possibility of (or fast workaround for) creating an object defined as derived a class without creating base class object in memory; instead the derived object should refer to the actually existing object of base class ("take-over" its memory residence)? This is needed for speed reasons - creating new a derived object, copying data from base class object to it, and then destroying base object takes too much time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

听风吹 2024-11-01 16:03:29

在这种情况下,您可能需要考虑组合而不是继承 - 这会更自然。

You might want to consider composition instead of inheritance in this case - it would be more natural.

a√萤火虫的光℡ 2024-11-01 16:03:29

我不会使用该语言支持的 class 构造。如果您需要一些小而灵活的东西,请考虑编写一个struct并使用函数指针实现您自己的v-table。与 Linux 内核中的情况大致相同。请注意,面向对象编程几乎可以用任何语言完成,不一定是支持它的语言。

然后,您可以动态切换虚拟表指针,并可能执行一些realloc,以添加派生类型所需的字段。

最后,您可以将所有这些打包在一个常规的类中,该类没有任何动态方法,只是将所有调用委托给所描述的内部结构。这不应该施加任何内存或计算开销。

编辑:实际上我想 realloc 不是正确的方法。这是一个涉及底层操作系统并需要上下文切换的例程。只要您已经分配了适当的内存块,几乎总是调用 copy 会更快。如果您对速度感兴趣,那么也许还可以考虑实现您自己的内存管理或使用 boost 等库提供的替代实现之一。

I wouldn't use the class construct that is supported by the language. If you need something small and that flexible consider writing a struct and implementing your own v-table's using function pointers. Much the same way as this is done for example in the Linux kernel. Note that object oriented programming can be done in almost any language, not necessarily one that supports it.

You could then switch the v-table pointer on the fly and possibly perform some realloc in order to add the fields that are required by the derived type.

In the end you could package all of this in a regular class that doesn't have any dynamic methods and just delegates all the calls to the described internal structure. This shouldn't impose any memory or computational overhead.

EDIT: Actually I guess realloc is not the way to go. This is a routine that engages the underlying operating system and requires a context switch. Almost always calling copy will be faster provided you have the appropriate memory block already allocated. If you're interested in speed, then maybe consider also implementing your own memory management or using one of the alternative implementation provided by libraries such as boost.

度的依靠╰つ 2024-11-01 16:03:29

我不认为你能做你想做的事。

考虑:

   d1 = prestochango(b);
   d2 = prestochango(b);
   d1.blarf = waldo;
   // what value does d2.blarf now have?

d1 和 d2 要么是不同的对象,包括不同的 b 基底,要么它们是同一个对象。

现在,您可以通过使您的 b-substrate 成为 d 类的静态成员来伪造它。

I don't think you can do what you seem to want to do.

Consider:

   d1 = prestochango(b);
   d2 = prestochango(b);
   d1.blarf = waldo;
   // what value does d2.blarf now have?

Either d1 and d2 are distinct objects, including distinct b-substrates, or they are the same object.

Now, you MIGHT be able to FAKE it by making your b-substrate a static member of your d class.

情归归情 2024-11-01 16:03:29

如果要动态调用派生类,v表是必不可少的。

因此,也许您可​​以实现没有数据成员的“基类”,并传入虚拟函数在调用期间作为参数所需的数据或数据指针。

它会节省内存,但会花费更多时间。

If you want dynamic call derived class, the v-table is indispensable.

So maybe you can implement the "base class" with no data member, and pass in data or pointer of data that your virtual functions need as argument during calling.

It'll save memory but cost more time.

一瞬间的火花 2024-11-01 16:03:29

如果您希望创建基类的一个版本并让所有对象继承或派生自同一个实例,那么您可以在基类中将事物声明为静态。静态意味着类的每个实例都有一个版本。

class FooBase {
protected:
    static int IDCnt;
    static int ObjCnt;
    int ID;
public:
    FooBase();
    ~FooBase();
    virtual int GetID();
    virtual int GetObjCnt();
    virtual int GetIDCnt();
};
//implementation
int FooBase::IDCnt = 0; //need to init static vars
int FooBase::ObjCnt = 0;
FooBase::FooBase() { ID = IDCnt; IDCnt++; ObjCnt++; }
FooBase::~FooBase() { ObjCnt--; }
int FooBase::GetID() { return ID; }
int FooBase::GetObjCnt() { return ObjCnt; }
int FooBase::GetIDCnt() { return IDCnt; }

#include "FooBase.h"
class FooDerived : public FooBase {
      //blah

};
#include "FooDervied.h"

int main() {
FooDerived A;
FooDerived B;
cout << A.GetID() << ' ' << A.GetObjCnt() << ' ' << A.GetIDCnt() << endl;
cout << B.GetID() << ' ' << B.GetObjCnt() << ' ' << B.GetIDCnt() << endl;
if(true) {
   FooDerived C;
   cout << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetObjCnt << C.GetIDCnt() << endl;
}
cout << B.GetObjCnt() << '' << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetIDCnt << endl;

}

以这种方式,您不声明基类项,而是通过静态变量继承基类的实例,这基本上意味着所有 FooDerived 都在查看相同的内存块 FooBase::IDCntFooBase::ObjCnt

If you wish to create one version of a base class and have all objects inherit or be derived from the same instance, then you can declare things as static in the base class. Static means one version of it for every instance of a class.

i.e.

class FooBase {
protected:
    static int IDCnt;
    static int ObjCnt;
    int ID;
public:
    FooBase();
    ~FooBase();
    virtual int GetID();
    virtual int GetObjCnt();
    virtual int GetIDCnt();
};
//implementation
int FooBase::IDCnt = 0; //need to init static vars
int FooBase::ObjCnt = 0;
FooBase::FooBase() { ID = IDCnt; IDCnt++; ObjCnt++; }
FooBase::~FooBase() { ObjCnt--; }
int FooBase::GetID() { return ID; }
int FooBase::GetObjCnt() { return ObjCnt; }
int FooBase::GetIDCnt() { return IDCnt; }

#include "FooBase.h"
class FooDerived : public FooBase {
      //blah

};
#include "FooDervied.h"

int main() {
FooDerived A;
FooDerived B;
cout << A.GetID() << ' ' << A.GetObjCnt() << ' ' << A.GetIDCnt() << endl;
cout << B.GetID() << ' ' << B.GetObjCnt() << ' ' << B.GetIDCnt() << endl;
if(true) {
   FooDerived C;
   cout << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetObjCnt << C.GetIDCnt() << endl;
}
cout << B.GetObjCnt() << '' << A.GetObjCnt() << ' ' << A.GetIDCnt() << ' ' << B.GetIDCnt << endl;

}

In this manner you don't declare a base class item, instead, the instance of base class is inherited through the static variables which basically means all FooDerived are looking at the same block of memory for FooBase::IDCnt and FooBase::ObjCnt.

跨年 2024-11-01 16:03:29

您考虑过工厂设计模式吗?

你的基类只需要知道你想创建什么样的派生类

Did you consider Factory design pattern ?

Your base class only needs to know what kinda derived class you wanna create

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文