默认参数和构造函数 [英] Default arguments and constructors

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

问题描述

如果想要将默认参数传递给构造函数,那么

最好的方法是什么,(除了重载构造函数并写一个

辅助函数) )?

MyClass MyClass(int,int);

MyClass MyClass(int,int,float);

void helper_func (int,int,float = PI);


我的意思是有人可以写这样的东西:


MyClass c * =新的MyClass(int arg1,int arg2,float arg3 = PI)?

(AFAIK,这是非法的,因为编译器不会理解它。


Tks

解决方案

Alfonso Morra写道:


如果想通过默认值构造函数的参数,最好的方法是什么,(除了重载构造函数和编写
辅助函数)?

MyClass MyClass(int,int) ;
MyClass MyClass(int,int,float);
void helper_func(int,int,float = PI);
我的意思是,无论如何都可以写出这样的东西:

MyClass c * = new MyClass(int arg1,int arg2,float arg3 = PI)?


因为什么时候在调用

a函数的地方指定了默认参数? :-)

(AFAIK,这是非法的,因为编译器不会理解它。




你试过吗?

这是完全合法的,任何编译器都应该接受它。


class MyClass

{

MyClass (int,int,float = PI);

};


-

Karl Heinz Buchegger
< a href =mailto:kb ****** @ gascad.at> kb ****** @ gascad.at




" Alfonso Morra" sw *********** @ the-ring.com>在消息中写道

如果想要传递默认值构造函数的参数,最好的方法是什么,(除了重载构造函数和编写
辅助函数)?

MyClass MyClass(int,int) ;
MyClass MyClass(int,int,float);
void helper_func(int,int,float = PI);




struct MyClass {


MyClass(int i,int j,float k = 3.14){

}

};


int main()

{

MyClass m1(1,2);

MyClass m2(1,2,2.28);

}


HTH,

Sharad


Alfonso Morra写道:

如果想要将默认参数传递给构造函数,最好的方法是什么,(除了重载构造函数)并写一个
辅助函数)?

MyClass MyClass(int,int);
MyClass MyClass(int,int,float);
void helper_func(int, int,float = PI);


相反,你可以在ctor本身做到这一点,


MyClass(int,int,float = PI);


为什么你重复两次班级名称?错字??

我的意思是有人可以写这样的东西:

MyClass c * = new MyClass(int arg1,int arg2,float arg3 = PI)?

(AFAIK,这是非法的,因为编译器不会理解它。

Tks




这是非法的 -


1)在向构造函数传递参数时不能指定类型

2)您必须将值传递给构造函数以构造< br $>
对象


MyClass * c = new MyClass(10,20);

MyClass * d = new MyClass(10,20) ,30.0);


问候,

Srini


If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?
(AFAIK, this is illegal, as the compiler dosen''t grok it.

Tks

解决方案

Alfonso Morra wrote:


If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?

Since when are default arguments specified at the place where
a function is invoked ? :-)
(AFAIK, this is illegal, as the compiler dosen''t grok it.



Have you tried it?
It is perfectly legal and any compiler should accept it.

class MyClass
{
MyClass( int, int, float = PI );
};

--
Karl Heinz Buchegger
kb******@gascad.at



"Alfonso Morra" <sw***********@the-ring.com> wrote in message

If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;



struct MyClass {

MyClass(int i, int j, float k = 3.14){
}
};

int main()
{
MyClass m1(1, 2);
MyClass m2(1, 2, 6.28);
}

HTH,
Sharad


Alfonso Morra wrote:

If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

Instead, you can do this in the ctor itself,

MyClass(int , int, float = PI);

Why have you repeated the class name twice?? Typo??
I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?
(AFAIK, this is illegal, as the compiler dosen''t grok it.

Tks



It is illegal -

1) You cannot specify types while passing arguments to a constructor
2) You have to pass values to the constructor for constructing an
object

MyClass *c = new MyClass(10, 20);
MyClass *d = new MyClass(10, 20, 30.0);

Regards,
Srini


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

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