Default argument

Discussion in 'C++' started by junw2000@gmail.com, Jan 16, 2009.

  1. Guest

    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.
     
    , Jan 16, 2009
    #1
    1. Advertising

  2. Guest

    On Jan 15, 11:55 pm, Jeff Schwab <> wrote:
    > 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.
     
    , Jan 16, 2009
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > On Jan 15, 11:55 pm, Jeff Schwab <> wrote:
    >> 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
     
    Ian Collins, Jan 16, 2009
    #3
  4. Guest

    On Jan 16, 12:51 am, Ian Collins <> wrote:
    > wrote:
    > > On Jan 15, 11:55 pm, Jeff Schwab <> wrote:
    > >> 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


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

    }
     
    , Jan 16, 2009
    #4
  5. Sana Guest

    On Jan 16, 12:33 pm, wrote:
    > On Jan 16, 12:51 am, Ian Collins <> wrote:
    >
    >
    >
    > > wrote:
    > > > On Jan 15, 11:55 pm, Jeff Schwab <> wrote:
    > > >> 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

    >
    > 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
    }

    --
    sana
     
    Sana, Jan 16, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    430
  2. Ben Kial
    Replies:
    1
    Views:
    727
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,989
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    342
  5. Reckoner
    Replies:
    11
    Views:
    742
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page