Default parameters of constructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin Vorbrodt

    Default parameters of constructors

    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

    #2
    Re: Default parameters of constructors


    "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
    news:KWK6b.2683 $PE6.965@newsre ad3.news.pas.ea rthlink.net...[color=blue]
    >
    > Marcin Vorbrodt <mvorbro@eos.nc su.edu> wrote in message
    > news:bjer80$906 $2@uni00nw.unit y.ncsu.edu...[color=green]
    > > 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[/color][/color]
    class[color=blue][color=green]
    > > methods as well... example:
    > >
    > > Constructor of my CoordinateFrame class:
    > >
    > > CoordinateFrame (const Basis &basis = Basis::ORIGIN() , const Point[/color][/color]
    &origin[color=blue]
    > =[color=green]
    > > Point::ORIGIN() );[/color]
    >
    > 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
    >
    >
    >[/color]

    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


    Comment

    • Marcin Vorbrodt

      #3
      Re: Default parameters of constructors


      "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
      news:obO6b.2841 $PE6.1823@newsr ead3.news.pas.e arthlink.net...[color=blue]
      >
      > Marcin Vorbrodt <mvorbro@eos.nc su.edu> wrote in message
      > news:bjfvuu$pro $1@uni00nw.unit y.ncsu.edu...[color=green]
      > >
      > > "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
      > > news:KWK6b.2683 $PE6.965@newsre ad3.news.pas.ea rthlink.net...[color=darkred]
      > > >
      > > > Marcin Vorbrodt <mvorbro@eos.nc su.edu> wrote in message
      > > > news:bjer80$906 $2@uni00nw.unit y.ncsu.edu...
      > > > > 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[/color]
      > > class[color=darkred]
      > > > > methods as well... example:
      > > > >
      > > > > Constructor of my CoordinateFrame class:
      > > > >
      > > > > CoordinateFrame (const Basis &basis = Basis::ORIGIN() , const Point[/color]
      > > &origin[color=darkred]
      > > > =
      > > > > 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
      > > >
      > > >
      > > >[/color]
      > >
      > > No, no compiler errors at all. I was just wondering if that was allowed[/color][/color]
      by[color=blue][color=green]
      > > the C++ standard, or simply a glitch in my compiler;-)[/color]
      >
      > 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
      >
      >
      >[/color]

      Great, cant wait to hear back from you.

      Martin


      Comment

      • Janusz Szpilewski

        #4
        Re: Default parameters of constructors

        Mike Wahler wrote:
        [color=blue]
        > 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. :-) )
        >[/color]

        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

        Comment

        • Mike Wahler

          #5
          Re: Default parameters of constructors

          Marcin Vorbrodt <mvorbro@eos.nc su.edu> wrote in message
          news:bjgc11$7oe $1@uni00nw.unit y.ncsu.edu...

          [using the return value of a static member function
          as a default function parameter]
          [color=blue][color=green][color=darkred]
          > > > No, no compiler errors at all. I was just wondering if that was[/color][/color][/color]
          allowed[color=blue]
          > by[color=green][color=darkred]
          > > > the C++ standard, or simply a glitch in my compiler;-)[/color]
          > >
          > > 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
          > >
          > >
          > >[/color]
          >
          > Great, cant wait to hear back from you.[/color]

          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



          Comment

          Working...