Default argument

  • Follow


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:













7/24/2012 7:40:54 AM


Reply: