Default argument

J

junw2000

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.
 
J

junw2000

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.
 
I

Ian Collins

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 :)
 
J

junw2000

#include <string>

struct MyClass
{
  std::string str;

  MyClass( std::string s = "HelloWorld" )
    : str(s) {}

};

Why post nonsense when real code is shorter :)

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);

}
 
S

Sana

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);

}

void func2( MyClass obj = MyClass())
{
// function body
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top