调用默认构造函数而不是参数化构造函数 [英] Default constructor called instead of parametrized constructor

查看:76
本文介绍了调用默认构造函数而不是参数化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个Weighting方案的类,并在该类中创建了4个具有不同数量参数的构造函数.当我尝试使用特定构造函数的参数调用构造函数时,还会调用不带参数的默认构造函数.我不明白为什么会这样.

I am implemented a class for Weighting scheme and have created 4 constructor in the class with different number of parameters.when i try to invoke the constructor with parameter of a particular constructor then also the default constructor with no parameters is called.I am unable to understand why such this is happening .

构造函数的定义:

593     UnigramLMWeight(double param_log_,int select_smoothing_,double param_smoothing1_,double param_smoothing2_)
594         : select_smoothing(select_smoothing_), param_log(param_log_), param_smoothing1(param_smoothing1_),
595           param_smoothing2(param_smoothing2_)
596         {

调用构造函数:

 79     enquire.set_weighting_scheme(Xapian::UnigramLMWeight(double(322.0),int(2),double(2000.0),double(2.0)));

但是我已经检查了设置的值是从默认构造函数中获取的.

But i have checked the values which are set are from default constructor.

有人能帮我为什么要调用此默认构造函数,还是每次在参数构造函数或将参数强制转换为其他类型后调用默认构造函数,构造函数都尝试找到此类构造函数,但无法找到此类构造函数的原因,以及最后调用默认构造函数.

can any one help me why this default constructor is called or is it every time default constructor is called after parametric constructor or the parameters are casted to some other type and constructor try to find such constructor but is unable to find such constructor and calls default constructor finally.

set_weigthing方案的代码为:

Code of set_weigthing scheme is :

 926 Enquire::set_weighting_scheme(const Weight &weight_)
 927 {
 928     LOGCALL_VOID(API, "Xapian::Enquire::set_weighting_scheme", weight_);
 929     // Clone first in case doing so throws an exception.
 930     Weight * wt = weight_.clone();
 931     swap(wt, internal->weight);
 932     delete wt;
 933 }

set_weighing scheme set方法是否调用克隆函数,这是克隆默认构造函数被调用时问题的根本原因吗?那可以是原因吗?

Do the set_weighing scheme set method calls clone function,do this is root couse of problem when it clones default constructor is called,is it so ? Can that be reason ?

推荐答案

创建对象副本(克隆?)时,将调用副本构造函数.似乎您尚未实现自定义副本构造函数,因此将调用由编译器生成的默认副本构造函数.

When you make a copy of the object (clone?) copy constructor gets called. Seems that you have not implemented a custom copy constructor so the default one generated by compiler is called instead.

UnigramLMWeight(const UnigramLMWeight& copy_from)
{
// implement copy here
}

http://login2win.blogspot.com/2008/05/c-copy-constructor.html 可能会有所帮助

这篇关于调用默认构造函数而不是参数化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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