Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c++ - Default constructor called instead of parametrized constructor

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 .

Definition of constructor:

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         {

Calling of constructor:

 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.

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 }

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 might be helpful


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...