Default arguments and constructors

Discussion in 'C++' started by Alfonso Morra, Sep 2, 2005.

  1. 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, Sep 2, 2005
    #1
    1. Advertising

  2. 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
    Karl Heinz Buchegger, Sep 2, 2005
    #2
    1. Advertising

  3. Alfonso Morra

    Srini Guest

    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
    Srini, Sep 2, 2005
    #3
  4. Alfonso Morra <> writes:

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


    what about this:

    class MyClass
    {
    MyClass(int a, int b, float c = PI) { ... }
    };

    MyClass *x = new MyClass(42, 0);



    or did I miss your question?


    --
    Philippe Amarenco, aka Phix
    epita 2007 - LSE - EpX
    "if new true friend not protected for explicit private union, break
    case and try using this." -- Nathan Myers, longest c++ sentence.
    Philippe Amarenco, Sep 2, 2005
    #4
  5. Alfonso Morra

    Sharad Kala Guest

    "Alfonso Morra" <> 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
    Sharad Kala, Sep 2, 2005
    #5
  6. Alfonso Morra

    Marc Mutz Guest

    Marc Mutz, Sep 2, 2005
    #6
  7. Srini wrote:
    > 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??


    Yes. Sorry, I left out the scope resolution operator in my haste. My bad.

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

    Thats what I thought - thanks for the clarification. I suppose I could
    still use overloaded constructors and a helper function though to make
    the library easier to use.
    Alfonso Morra, Sep 2, 2005
    #7
  8. Marc Mutz wrote:

    > Alfonso Morra wrote:
    > <snip>
    >
    >>MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI)

    >
    > <snip>
    >
    > Assuming you really mean what you wrote there,
    > Boost.Parameter is what you want:
    > http://www.boost.org/libs/parameter/doc/html/index.html
    >
    > Marc
    >
    >


    Thanks for the link. I think I'll use this in another project. But for
    now, thats not quite what I asked for in my OP. But thanks any how.
    Alfonso Morra, Sep 2, 2005
    #8
  9. Philippe Amarenco wrote:

    > Alfonso Morra <> writes:
    >
    >
    >>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.
    >>

    >
    >
    > what about this:
    >
    > class MyClass
    > {
    > MyClass(int a, int b, float c = PI) { ... }
    > };
    >
    > MyClass *x = new MyClass(42, 0);
    >
    >
    >
    > or did I miss your question?
    >
    >


    Phil, did you try to compile this?.
    Alfonso Morra, Sep 2, 2005
    #9
  10. Alfonso Morra wrote:
    >
    >> >>(AFAIK, this is illegal, as the compiler dosen't grok it.
    > >>

    > >
    > >
    > > what about this:
    > >
    > > class MyClass
    > > {
    > > MyClass(int a, int b, float c = PI) { ... }
    > > };
    > >
    > > MyClass *x = new MyClass(42, 0);
    > >
    > >
    > >
    > > or did I miss your question?
    > >
    > >

    >
    > Phil, did you try to compile this?.


    Of course he did not as this is incomplete code (as was your posted
    example).

    Instead of letting us guess, why don't you tell us what your
    compilers error message is. I bet it has something to do
    with the way you defined 'PI'.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 2, 2005
    #10
  11. Karl Heinz Buchegger wrote:

    > Alfonso Morra wrote:
    >
    >>>>>(AFAIK, this is illegal, as the compiler dosen't grok it.
    >>>>
    >>>
    >>>what about this:
    >>>
    >>>class MyClass
    >>>{
    >>> MyClass(int a, int b, float c = PI) { ... }
    >>>};
    >>>
    >>>MyClass *x = new MyClass(42, 0);
    >>>
    >>>
    >>>
    >>>or did I miss your question?
    >>>
    >>>

    >>
    >>Phil, did you try to compile this?.

    >
    >
    > Of course he did not as this is incomplete code (as was your posted
    > example).
    >
    > Instead of letting us guess, why don't you tell us what your
    > compilers error message is. I bet it has something to do
    > with the way you defined 'PI'.
    >

    This issue has been resolved. See my response to Srini's post.
    Alfonso Morra, Sep 2, 2005
    #11
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,271
    Martijn Lievaart
    Feb 6, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    4,934
    Josiah Carlson
    Apr 6, 2004
  3. Jeremy Smith
    Replies:
    2
    Views:
    586
    Jeremy Smith
    Aug 3, 2006
  4. Jess
    Replies:
    5
    Views:
    595
    Ron Natalie
    Jun 7, 2007
  5. Peng Yu
    Replies:
    5
    Views:
    392
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page