Windows平台下cpp中的构造函数的小帮助 [英] small help about the constructor in cpp under windows platform
本文介绍了Windows平台下cpp中的构造函数的小帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
先生.
我创建了一个包含一个默认值和一个参数构造函数的类.
sir.
i create a class which containing one default and one parameter constructor..
class cSimple
{
private:
int icount;
int iLoopValue;
public: //always constructure must be in public
cSimple()
{
icount=0;
}
cSimple(int iTemp)
{
iLoopValue=iTemp;
}
void MyFunction()
{
for(;icount<iLoopValue;icount++)
cout<<"The count value is :"<<icount<<endl;
}
};
我的疑问:
我可以创建一个对象来调用两个构造函数吗?.
如果我这样做就意味着它无法正常运行..您能给出解决方案/
my doubt :
can i create a one single object to make a call to both the constructor..
if i done like that means it not running properly .. can you give the solution/
if it possible to call both the constructor with using single object..
推荐答案
cSimple()
{
if(CommonInit() != 0){error handling}
}
cSimple(int iTemp)
{
if(CommonInit() != 0){error handling}
iLoopValue=iTemp;
}
int CSimple::CommonInit()
{
icount = 0;
iLoopValue = 0; //Don''t leave any members uninitialized
return 0;
}
,您不能调用MyFunction( )在构造函数中,但是如果发生严重错误或无法进行错误处理,这将很危险
最好是默认arg:
cSimple(int iTemp = 0)
you cant call MyFunction() inside the constructor, but this is dangerous if severe errors are happening or no error handling possible
best is default arg:
cSimple(int iTemp = 0)
您可以为已创建的对象显式调用构造函数:
You can call a constructor explicitly for an already created object:
cSimple myS; // Normal constructor call, creates the object
myS.cSimple::cSimple(134); // Explicit constuctor call on an existing object
这样,您可以在同一个对象上调用多个构造函数.这不是推荐的做法.
This way you can call several constructors on the same object. This not a recommended practice.
这篇关于Windows平台下cpp中的构造函数的小帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文