我正在编写一个函数,将某个类( 2d直方图,TH2F*)的内容复制到另一个TH2F*。实际上,我想要
SafeCopy( in, out )其中in是我的输入,TH2F*和out是我的目的地TH2F*。特别是,我希望以这样的方式实现SafeCopy,以便在以前还没有分配out的情况下也能工作。在第一个例子中,我以这样(错误)的方式实现了这个函数。
void SafeCopy( const TH2F * h_in, TH2F *h_out )
{
cout << "SafeCopy2d: output histogram address is " << h_out << endl;
if( h_out != NULL )
{
cout << "SafeCopy2d: h_out has been identified as non-zero pointer\n";
(*h_out) = *h_in; // I'm making use of the copy-constructor
// it wouldn't work if h_out == NULL
}
else
{
cout << "SafeCopy2d: h_out has been identified as null pointer\n";
h_out = new TH2F( *h_in );
cout << "SafeCopy2d: h_out address is now " << h_out << endl;
}
}输出是
SafeCopy2d: output histogram address is 0x0
SafeCopy2d: h_out has been identified as null pointer
SafeCopy2d: h_out address is now 0xblahblah但这当然行不通,因为在退出函数时,“真正的”指针h_out仍然是0,因为我通过复制而不是引用传递它。然后,我将函数的原型(不改变其实现)更改为
void SafeCopy( const TH2F * h_in, TH2F *&h_out )以便通过引用传递h_out指针。在后一种情况下,会发生一些奇怪的事情:如果调用SafeCopy传递一个空h_out,就会得到以下输出:
SafeCopy2d: output histogram address is 0x*a non-zero value*
SafeCopy2d: h_out has been identified as non-zero pointer我的问题是:如果我通过复制传递h_out,为什么它被正确地识别为空指针,而当我通过引用传递它时,它显示为非零?
编辑--这是调用代码:
//TH2F * h_migration is created and filled previously in the program
TH2F * h_smearedMigration;//
for (int ntoy=0; ntoy < NTOY; ntoy++ ) {
//matrix smearing
SmartCopy( h_migration, h_smearedMigration ); //copy the original matrix to a temporary one
RunToy( h_smearedMigration ); //smear the matrix
...我想避免这样的事情
h_smearedMigration = SmartCopy( h_migration, h_smearedMigration );发布于 2013-03-27 19:24:39
首先,还不清楚为什么要使用指针。理想情况下,您只想直接持有对象。那么,您就不需要一个特殊的公约来复制:
TH2F RunToy(TH2F const &in);
//TH2F h_migration is created and filled previously in the program
for (int ntoy=0; ntoy < NTOY; ntoy++ ) {
TH2F h_smearedMigration = RunToy(h_migration);如果TH2F的复制成本很高,那么您可以通过类似于pImple的方法来实现它,这样就可以便宜地移动它,但仍然可以充当一个值类型。
如果您确实需要指针,那么您应该使用智能指针,而不要拥有原始指针(拥有原始指针几乎可以保证代码异常--例如,不安全)。
void RunToy(std::unique_ptr<TH2F> in_out);
void SafeCopy(TH2F const &in, std::unique_ptr<TH2F> &out)
{
if(h_out) {
*out = in; // copy assignment
} else {
out = make_unique<TH2F>(h_in); // copy construction
}
}
for (int ntoy=0; ntoy < NTOY; ntoy++ ) {
std::unique_ptr<TH2F> h_smearedMigration;
SmartCopy(h_migration, h_smearedMigration);
RunToy(h_smearedMigration );当然,通常不需要使用SmartCopy函数来动态确定是使用复制分配还是复制构造。根据您是否已经分配了对象,您应该知道您需要什么。
// std::unique_ptr<TH2F> h_migration is created and filled previously in the program
for (int ntoy=0; ntoy < NTOY; ntoy++ ) {
auto h_smearedMigration = make_unique<TH2F>(*h_migration);
RunToy(h_smearedMigration);发布于 2013-03-27 18:26:33
您还没有显示调用代码,但听起来问题来自于类似于SafeCopy(something, NULL)的东西。是的,没用的。传入指针并返回结果:
TH2F *SafeCopy(constTH2F *in, TH2F *out) {
if (!out)
out = whatever;
*out = *in; // or whatever...
return out;
}发布于 2013-03-27 18:37:44
如果调用SafeCopy传递空h_out,则得到以下输出: ..。 *编辑这是调用代码: TH2F * h_smearedMigration; ..。 SmartCopy( h_migration,h_smearedMigration );
这不是空指针,是未初始化的指针。它包含随机垃圾,不太可能是0x0。
https://stackoverflow.com/questions/15666454
复制相似问题