Default parameters of constructors

Discussion in 'C++' started by Marcin Vorbrodt, Sep 7, 2003.

  1. I know that the default parameter(s) of a constructor can be either
    hardcoded values or function calls. But can they be calls to static class
    methods as well... example:

    Constructor of my CoordinateFrame class:

    CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point &origin =
    Point::ORIGIN());

    Thanks,
    Martin
     
    Marcin Vorbrodt, Sep 7, 2003
    #1
    1. Advertising

  2. "Mike Wahler" <> wrote in message
    news:KWK6b.2683$...
    >
    > Marcin Vorbrodt <> wrote in message
    > news:bjer80$906$...
    > > I know that the default parameter(s) of a constructor can be either
    > > hardcoded values or function calls. But can they be calls to static

    class
    > > methods as well... example:
    > >
    > > Constructor of my CoordinateFrame class:
    > >
    > > CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point

    &origin
    > =
    > > Point::ORIGIN());

    >
    > Are you getting compiler errors or what? If so, what are they,
    > and what exact code causes them?
    >
    > #include <iostream>
    >
    > class A
    > {
    > public:
    > static int foo() { return 1; }
    > };
    >
    > class B
    > {
    > public:
    > static int foo() { return 2; }
    > };
    >
    > class C
    > {
    > public:
    > int membera;
    > int memberb;
    >
    > C(int parma = A::foo(), int parmb = B::foo())
    > : membera(parma), memberb(parmb) {}
    > };
    >
    > int main()
    > {
    > C c;
    > std::cout << c.membera << ", " << c.memberb << '\n';
    > return 0;
    > }
    >
    > Output:
    >
    > 1, 2
    >
    > -Mike
    >
    >
    >


    No, no compiler errors at all. I was just wondering if that was allowed by
    the C++ standard, or simply a glitch in my compiler;-)

    Martin
     
    Marcin Vorbrodt, Sep 7, 2003
    #2
    1. Advertising

  3. "Mike Wahler" <> wrote in message
    news:eek:bO6b.2841$...
    >
    > Marcin Vorbrodt <> wrote in message
    > news:bjfvuu$pro$...
    > >
    > > "Mike Wahler" <> wrote in message
    > > news:KWK6b.2683$...
    > > >
    > > > Marcin Vorbrodt <> wrote in message
    > > > news:bjer80$906$...
    > > > > I know that the default parameter(s) of a constructor can be either
    > > > > hardcoded values or function calls. But can they be calls to static

    > > class
    > > > > methods as well... example:
    > > > >
    > > > > Constructor of my CoordinateFrame class:
    > > > >
    > > > > CoordinateFrame(const Basis &basis = Basis::ORIGIN(), const Point

    > > &origin
    > > > =
    > > > > Point::ORIGIN());
    > > >
    > > > Are you getting compiler errors or what? If so, what are they,
    > > > and what exact code causes them?
    > > >
    > > > #include <iostream>
    > > >
    > > > class A
    > > > {
    > > > public:
    > > > static int foo() { return 1; }
    > > > };
    > > >
    > > > class B
    > > > {
    > > > public:
    > > > static int foo() { return 2; }
    > > > };
    > > >
    > > > class C
    > > > {
    > > > public:
    > > > int membera;
    > > > int memberb;
    > > >
    > > > C(int parma = A::foo(), int parmb = B::foo())
    > > > : membera(parma), memberb(parmb) {}
    > > > };
    > > >
    > > > int main()
    > > > {
    > > > C c;
    > > > std::cout << c.membera << ", " << c.memberb << '\n';
    > > > return 0;
    > > > }
    > > >
    > > > Output:
    > > >
    > > > 1, 2
    > > >
    > > > -Mike
    > > >
    > > >
    > > >

    > >
    > > No, no compiler errors at all. I was just wondering if that was allowed

    by
    > > the C++ standard, or simply a glitch in my compiler;-)

    >
    > Well, it's good that you check. I'm fairly sure it's valid, I'll
    > have to check the standard to be sure. No time right now, but
    > I'll post my findings when I return (unless of course someone else
    > beats me to it. :) )
    >
    > -Mike
    >
    >
    >


    Great, cant wait to hear back from you.

    Martin
     
    Marcin Vorbrodt, Sep 7, 2003
    #3
  4. Mike Wahler wrote:

    > Well, it's good that you check. I'm fairly sure it's valid, I'll
    > have to check the standard to be sure. No time right now, but
    > I'll post my findings when I return (unless of course someone else
    > beats me to it. :) )
    >


    The C++ standard forbids the default parameters being local variables
    (8.3.6/7) or the keyword this (8.3.6/8). I cannot think of any inherent
    reason why constructor might clash with a static member function. Seen
    from the run-time environment point of view they are completely unrelated.

    Regards,
    Janusz
     
    Janusz Szpilewski, Sep 8, 2003
    #4
  5. Marcin Vorbrodt

    Mike Wahler Guest

    Marcin Vorbrodt <> wrote in message
    news:bjgc11$7oe$...

    [using the return value of a static member function
    as a default function parameter]

    > > > No, no compiler errors at all. I was just wondering if that was

    allowed
    > by
    > > > the C++ standard, or simply a glitch in my compiler;-)

    > >
    > > Well, it's good that you check. I'm fairly sure it's valid, I'll
    > > have to check the standard to be sure. No time right now, but
    > > I'll post my findings when I return (unless of course someone else
    > > beats me to it. :) )
    > >
    > > -Mike
    > >
    > >
    > >

    >
    > Great, cant wait to hear back from you.


    What you're doing is valid, according to 8.3.6
    Also note that this issue does not only apply
    to constructors, but to any function.

    -Mike
     
    Mike Wahler, Sep 8, 2003
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,331
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    603
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    615
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    403
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    482
Loading...

Share This Page