General discussion

All Comments

  • Author
    Replies
    • #2681324

      Feedback about default argument values tip

      by maryweilage ·

      In reply to C++ Forum (Continued)

      Note from the editor: The following comments are from member Walter Tross about the Feb. 25th edition of the C++ e-newsletter entitled, “Default argument values in pointers to functions and typedefs.”

      Your article about default arguments is fine. Just one thing: you forgot to tell us WHAT the standard says. If I remember well, it says something like default arguments can appear only in one place for the same function in the same compilation unit – but I could be wrong.
      The idea of talking about proprietary extensions to the standard which can make your code nonportable is good, I would like to read more about this. I for myself, e.g., always painstakingly re-enable for loop scope conformity every time I start a new project in Visual C++ (/Zc:forScope). I hate the fact that Microsoft lets you write this code by default:

      for (int i=0; i

      • #2681209

        default arguments

        by dannyk ·

        In reply to Feedback about default argument values tip

        The issue of standard extensions is very wide and I intend to cover some of the most common features in future tips.
        As for default argument, the Standard says quite a lot (in fact, too much, IMHO).

        A default value may appear in a function declaration or definition. It is possible to have multiple declarations with different default values so long as the overriding declaration appears in an enclosed namespace of an outer namesapce. In other words, you can have something like this:

        void f(int x, int y=0); //#1

        int func()
        {
        void f(int x, int y=8); //#2, fine, overrides #1
        void f(int x, int y=9); //#3, illegal

        f(8); //f(8,8); becaue of #2
        }

        That said, such programming style is disatrous. My advice is to define default values only once, in a function’s prototype.

        Placing default values in a typedef, pointer to function or pointer to member isn’t allowed according to the Standard.

        Danny Kalev

        • #2667646

          Sorry to say it, you’re hoist on your won petard

          by tre ·

          In reply to default arguments

          A default value may appear in a function declaration or definition. It is possible to have multiple declarations with different default values so long as the overriding declaration appears in an enclosed namespace of an outer namesapce. In other words, you can have something like this:

          void f(int x, int y=0); //#1

          int func()
          {
          void f(int x, int y=8); //#2, fine, overrides #1
          // redfinition of function f - in the same NAMESPACE
          void f(int x, int y=9); //#3, illegal

          f(8); //f(8,8); becaue of #2
          }

        • #2667643

          Dyslexic fingers

          by tre ·

          In reply to Sorry to say it, you’re hoist on your won petard

          Should be: OWN petard

    • #3381774

      C++ Question

      by kellykey ·

      In reply to C++ Forum (Continued)

      What is the C++ equivilent to a Class.forName command in Java?

      • #2695927

        good question…

        by dannyk ·

        In reply to C++ Question

        Standard C++ doesn’t have an equivalent function. Dynamic loading in general isn’t standardized in C++ so each implementation defines its own APIs and libraries for dynamic loading. If you’re interested in reflection per se, there’s a quasi-standard reflection on http://www.boost.org that offers a similar mechanism.

        Danny

        • #2667642

          ForName

          by tre ·

          In reply to good question…

          forName returns the Class object associated with the class or interface with the given string name, using the given class loader.

          This is equivalent to the macro ‘RUNTIME_CLASS’ in MFC – I presume there is an equivalent macro/funtion/statement for UNIX/LINUX implementions of windowing objects for C/C++.

Viewing 1 reply thread