|
|
Default argument
In the function below:
void func1( int a = 10 )
{
//function body
}
The variable a is a default argument.
For the function below:
void func2( MyClass obj ) //MyClass is a defined class.
{
//function body
}
How to make the variable obj a default argument?
Thanks.
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
1/16/2009 7:54:23 AM |
|
junw2000@gmail.com wrote:
> For the function below:
>
> void func2( MyClass obj ) //MyClass is a defined class.
> How to make the variable obj a default argument?
What do you want the default value to be?
|
|
0
|
|
|
|
Reply
|
jeff34 (1595)
|
1/16/2009 7:55:53 AM
|
|
On Jan 15, 11:55=A0pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> junw2...@gmail.com wrote:
> > For the function below:
>
> > void func2( MyClass obj ) =A0 //MyClass is a defined class.
> > How to make the variable obj a default argument?
>
> What do you want the default value to be?
For example below is an implementation of MyClass (just for demo
purpose):
class MyClass
{
public:
MyClass( stl::string s ) { str =3D s; }
stl::string str;
}
I want the default value to be: MyClass("HelloWorld").
Thanks.
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
1/16/2009 8:05:39 AM
|
|
junw2000@gmail.com wrote:
> On Jan 15, 11:55 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
>> junw2...@gmail.com wrote:
>>> For the function below:
>>> void func2( MyClass obj ) //MyClass is a defined class.
>>> How to make the variable obj a default argument?
>> What do you want the default value to be?
>
> For example below is an implementation of MyClass (just for demo
> purpose):
>
> class MyClass
> {
> public:
> MyClass( stl::string s ) { str = s; }
>
> stl::string str;
> }
>
> I want the default value to be: MyClass("HelloWorld").
>
#include <string>
struct MyClass
{
std::string str;
MyClass( std::string s = "HelloWorld" )
: str(s) {}
};
Why post nonsense when real code is shorter :)
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9912)
|
1/16/2009 8:51:01 AM
|
|
On Jan 16, 12:51=A0am, Ian Collins <ian-n...@hotmail.com> wrote:
> junw2...@gmail.com wrote:
> > On Jan 15, 11:55 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> >> junw2...@gmail.com wrote:
> >>> For the function below:
> >>> void func2( MyClass obj ) =A0 //MyClass is a defined class.
> >>> How to make the variable obj a default argument?
> >> What do you want the default value to be?
>
> > For example below is an implementation of MyClass (just for demo
> > purpose):
>
> > class MyClass
> > {
> > public:
> > MyClass( stl::string s ) { str =3D s; }
>
> > stl::string str;
> > }
>
> > I want the default value to be: MyClass("HelloWorld").
>
> #include <string>
>
> struct MyClass
> {
> =A0 std::string str;
>
> =A0 MyClass( std::string s =3D "HelloWorld" )
> =A0 =A0 : str(s) {}
>
> };
>
> Why post nonsense when real code is shorter :)
>
> --
> Ian Collins
I just want to know how to set the default argument for the function
below:
void func2( MyClass obj ) //MyClass is a defined class.
{
//function body
}
so that the following code works:
void main()
{
func2(); //No argument passed.
MyClass aa;
func2(aa);
}
|
|
0
|
|
|
|
Reply
|
junw2000 (221)
|
1/16/2009 5:33:40 PM
|
|
junw2000@gmail.com wrote:
> On Jan 15, 11:55 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
>> junw2...@gmail.com wrote:
>>> For the function below:
>>> void func2( MyClass obj ) //MyClass is a defined class.
>>> How to make the variable obj a default argument?
>> What do you want the default value to be?
>
> For example below is an implementation of MyClass (just for demo
> purpose):
>
> class MyClass
> {
> public:
> MyClass( stl::string s ) { str = s; }
>
> stl::string str;
> }
>
> I want the default value to be: MyClass("HelloWorld").
>
> Thanks.
void func(MyClass obj ="HelloWorld");
|
|
0
|
|
|
|
Reply
|
jeff34 (1595)
|
1/16/2009 7:12:33 PM
|
|
On Jan 16, 12:33=A0pm, junw2...@gmail.com wrote:
> On Jan 16, 12:51=A0am, Ian Collins <ian-n...@hotmail.com> wrote:
>
>
>
> > junw2...@gmail.com wrote:
> > > On Jan 15, 11:55 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> > >> junw2...@gmail.com wrote:
> > >>> For the function below:
> > >>> void func2( MyClass obj ) =A0 //MyClass is a defined class.
> > >>> How to make the variable obj a default argument?
> > >> What do you want the default value to be?
>
> > > For example below is an implementation of MyClass (just for demo
> > > purpose):
>
> > > class MyClass
> > > {
> > > public:
> > > MyClass( stl::string s ) { str =3D s; }
>
> > > stl::string str;
> > > }
>
> > > I want the default value to be: MyClass("HelloWorld").
>
> > #include <string>
>
> > struct MyClass
> > {
> > =A0 std::string str;
>
> > =A0 MyClass( std::string s =3D "HelloWorld" )
> > =A0 =A0 : str(s) {}
>
> > };
>
> > Why post nonsense when real code is shorter :)
>
> > --
> > Ian Collins
>
> I just want to know how to set the default argument for the function
> below:
> void func2( MyClass obj ) =A0 //MyClass is a defined class.
> {
> //function body
>
> }
>
> so that the following code works:
>
> void main()
> {
>
> func2(); //No argument passed.
>
> MyClass aa;
>
> func2(aa);
>
> }
void func2( MyClass obj =3D MyClass())
{
// function body
}
--
sana
|
|
0
|
|
|
|
Reply
|
sanatakos (21)
|
1/16/2009 8:22:56 PM
|
|
|
6 Replies
187 Views
(page loaded in 0.461 seconds)
Similiar Articles: How to pass enum as default argument - comp.lang.c++.moderated ...I am having an issue with passing an enum as a default argument to a class method. I have a class similar to enum Method { a, b }; class A{ A()... C++0X explicitly defaulted copy constructors - comp.lang.c++ ...See below for the code. A variadic argument U&&... matches a copy-from-lvalue better than the implicit copy constructors, so I need to declare a cop... how to pass command line arguments to makefile - comp.unix ...How to pass enum as default argument - comp.lang.c++.moderated ... how to pass command line arguments to makefile - comp.unix ... How to pass enum as default argument ... When does a base member variable's name come into scope? - comp ...In case #1 the default argument initalizer can't be A::_i because you mustn't use a nonstatic member for that (see Standard 8.2.6). In case #2 you initialize (via c'tor ... How to pass user name and password when telnet to a machine - comp ...How to pass enum as default argument - comp.lang.c++.moderated ... How to pass user name and password when telnet to a machine - comp ... Hello, How I can pass username ... Finding the last arg passed to a function - comp.lang.asm.x86 ...For the fixed number, both sides know in advance how many arguments there will be. ... How to pass enum as default argument - comp.lang.c++.moderated ... There is a chance that ... Floating Point and printf() - comp.lang.asm.x86The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are ... enum and operator++ - comp.lang.c++I am having an issue with passing an enum as a default argument to a class method. I have a class similar to enum Method { a, b }; class A{ A()... Using enum and specific values in method. - comp.lang.java.help ...Using enum and specific values in method. - comp.lang.java.help ... How to pass enum as default argument - comp.lang.c++.moderated ... I have a class similar to enum ... Passing va_list by reference to a function - comp.lang.c ...My workaround was to copy the ap argument to a va_list variable in xprintf() and ... How to pass enum as default argument - comp.lang.c++.moderated ... Passing va_list by ... Default argument - Wikipedia, the free encyclopediaIn computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may ... Default Arguments (C++) - Microsoft Corporation: Software ...In many cases, functions have arguments that are used so infrequently that a default value would suffice. To address this, the default-argument facility allows for ... 7/24/2012 7:40:54 AM
|
|
|
|
|
|
|
|
|