CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Mar 2006
    Posts
    7

    Question defalut arguments

    why default arguments must be trialing arguments?
    why can't be as leading or middle arguments in argument list?

  2. #2
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: defalut arguments

    ?? what are you trieing to say

  3. #3
    Join Date
    Feb 2006
    Location
    London
    Posts
    238

    Re: defalut arguments

    To avoid problems with ambiguity. Suppose you have function of 5 integer arguments and second and forth are optional. In the program this function is caleed with 4 parameters. How can compiler find out which parameter has to have the default value

  4. #4
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: defalut arguments

    after the second all parameters must be have a default variable. on my compiler atleast

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: defalut arguments

    Quote Originally Posted by Mitsukai
    after the second all parameters must be have a default variable. on my compiler atleast
    On all compilers. I believe it might even be a part of the standard.

    As Dragforce said, this is to avoid ambiguity.
    Code:
    void someFunc(int a, int b, int c = 4, int d = 5, int e);
    ...
    someFunc(10, 11, 13, 14);
    In the call to 'someFunc' what paramaters get what values? Which one is the parameter that I wanted to have the default value?

    VIggy

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: defalut arguments

    It is, indeed, part of the standard. Personally, I'd like the standard to allow something like
    Code:
    someFunc(10, 11, , 13, 14);
    that is, args are matched strictly in order, with a blank argument meaning "accept the default". In this case, Viggy's example would be an error, because there's no match for "e".
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: defalut arguments

    Quote Originally Posted by Graham
    It is, indeed, part of the standard. Personally, I'd like the standard to allow something like
    Yes, IMHO, it would not complicate much the language, and could be a real benefit.
    An, alternative would be:
    Code:
    someFunc(10,11,default,13,14);
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: defalut arguments

    C++ stndards shuld also allow static class operator and a parent class operator.

  9. #9
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: defalut arguments

    Quote Originally Posted by Mitsukai
    C++ stndards shuld also allow static class operator and a parent class operator.
    I don't see any practical application of static member operators.
    The this parameter is an implicit parameter of the operator.
    Would you want to have an operator whose one parameter (maybe the only parameter) is missing?
    Except, in some very particular cases, for operator () in functors.
    And, in that case, a non-static operator () does not harm!

    parent class operator?
    I don't see what you mean.
    Perhaps the fact that operators are inherited?
    But, actually operators are already inherited!
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: defalut arguments

    in my OOP programming OpenGL i had a need for static operators! just that its not logical doesntmean its not needed, thats why C++ got big in first place, the flexibility and the usefullness, so why limit operators to non-statics.
    and with parent i mean this:
    class A{ int c; class B{ B(void){ parent->c = 123 } }; };

  11. #11
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: defalut arguments

    For this to make sense enclosed class must actually have parent object, which it doesn't. You can provide it directly simply enough.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: defalut arguments

    Quote Originally Posted by Mitsukai
    in my OOP programming OpenGL i had a need for static operators
    Please share the code!

    Quote Originally Posted by Mitsukai
    class A{ int c; class B{ B(void){ parent->c = 123 } }; };
    You misunderstood the concept of nested class!
    There is no one-to-one correspondance between instances of A and B!
    A and B have independent instances.
    Declaring B inside A, have effect only on the scope of B.

    In order to have this one-to-one correspondance, you can simply pass the this pointer of A in a constructor of B:
    Code:
    class A
    {
    int c; // note that c must precede b in this class definition, otherwise, behaviour is undefined!
    B b;
    class B{ B(A* parent){ parent->c = 123 } };
    A():b(this) {}
    };
    I already had something similar.
    IMHO, it is perfectly clean, and does not require any language change!

    I experienced similar problems that were perfectly resolved by private inheritance (knowing that C++ allows multiple inheritance).

    It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  13. #13
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: defalut arguments

    Quote Originally Posted by SuperKoko
    It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
    It's OK for C++.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  14. #14
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: defalut arguments

    Quote Originally Posted by SuperKoko
    Please share the code!


    You misunderstood the concept of nested class!
    There is no one-to-one correspondance between instances of A and B!
    A and B have independent instances.
    Declaring B inside A, have effect only on the scope of B.

    In order to have this one-to-one correspondance, you can simply pass the this pointer of A in a constructor of B:
    Code:
    class A
    {
    int c; // note that c must precede b in this class definition, otherwise, behaviour is undefined!
    B b;
    class B{ B(A* parent){ parent->c = 123 } };
    A():b(this) {}
    };
    I already had something similar.
    IMHO, it is perfectly clean, and does not require any language change!

    I experienced similar problems that were perfectly resolved by private inheritance (knowing that C++ allows multiple inheritance).

    It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
    i know about that, i sayd C++ shuld have a parent operator without having the need of the constructor like you just did

  15. #15
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: defalut arguments

    Quote Originally Posted by RoboTact
    It's OK for C++.
    C++ has a few useless features inherited from C, but otherwise, has a full set of general purpose features that allow anybody to write very high-level (i.e. very abstract) code.
    From that, C++ is extended by librairies!
    No need for builtin smart pointers : A library can emulated it!
    No need for builtin dynamic array : there is std::vector!
    etc...
    With templates, you can really do many things that are builtin in other languages.

    Quote Originally Posted by Mitsukai
    i know about that, i sayd C++ shuld have a parent operator without having the need of the constructor like you just did
    The problem, is that this thing would be only used by very few programmers.
    This feature does not increase the abstraction level, nor increas maintainability. That is, the workaround does loose nothing.
    It is a bit more verbose, but that is not a problem. It will only increase a bit your program (except if your program is full of design flaws), at the cost of your fingers.

    Hundreds other minor features avoiding manual writing in very specific cases, are proposed on comp.std.c++.
    But there are far too many. That would increase the language specification by a factor ten!

    C++ must be learnable and implementable and extensible!
    Nowadays, C++ is already very extensible (the boost library is a proof).
    The future C++ standard will focus more on librairies than the core language.

    If you want to preserve your fingers from typing, you'll gain much more, using these horrible macos:
    Code:
    #define cl class
    #define fr for
    #define wl while
    typedef char ch;
    typedef int it;
    typedef double dl;
    // ...
    Last edited by SuperKoko; April 11th, 2006 at 04:09 PM.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured