指针功能的深层复制 [英] Deep copy of a pointer function

查看:97
本文介绍了指针功能的深层复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类(myClass),该类的指针可以像这样充当私有成员:

I have a class (myClass) that has a pointer to function as private member like this:

void (gi::myClass::*ptrFunction) (int  , int  , vector<int> & , vector<int> & , vector<int> &);

在运行时(带有新对象)创建myClass的对象(* myClass ptrObject)时,此指针在构造函数中使用函数的地址初始化。
之后,使用上面创建的相同对象,我调用了另一个函数。
在此函数中,我必须创建调用对象的深层副本:

When you create at runtime (with new) a object (a *myClass ptrObject) of myClass this pointer is inizialited in the constructor with the address of a function. After with the same object created above I call another function. In this function I must create a deep copy of the calling object:

gi::myClass *ptrObject2 = new myClass(....);

上面我称一个构造函数执行复制,但是它不初始化成员* ptrFunction * ptrObject2
我的问题是:
如何将指针函数从* ptrObject复制到ptrObject2?
(我的编译器是C ++ 11)

Above I call a constructor that do the copy but it don't initialize the member *ptrFunction of *ptrObject2 My question is: How do I do a deep copy of the pointer function from the *ptrObject to ptrObject2? (My compiler is C++11)

推荐答案

没有深拷贝函数指针。您没有复制任何数据,只是将引用复制到v表中。这样:

There is no deep-copy of a function pointer. You are not copying any data, just a reference into the v-table. As such:

ptrObject2->ptrFunction = ptrObject->ptrFunction

很好。

这篇关于指针功能的深层复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆