Calling one constructor from another in VC++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bern McCarty

    #31
    Re: Calling one constructor from another in VC++

    Hmmmm. Why do you say that explicit qualification is bogus/illegal from a
    Standard C++ point of view? I can buy that using it to call another
    constructor on a partially constructed object might be a mistake, but I
    question the "even it it were legal" and "its non-standard" assertions.
    Explicit qualification appears to be perfectly legal and standard C++. Is
    it not? From Section 10.2 "Member name lookup" of the C++ standard

    12 Explicit qualification with the scope operator (5.1) suppresses the
    virtual call mechanism. [Example:

    class B { public: virtual void f(); };

    class D : public B { public: void f(); };

    void D::f() { /* ... */ B::f(); }

    Here, the function call in D::f really does call B::f and not D::f. ]


    And there are other places in the standard where explicit qualification is
    used to make various points. I easily believe that using explicit
    qualification to call one constructor from another is bad, but I would like
    to better understand why. My own guess is that it would be bad because it
    could cause double invocation of constructors for all nodes of the
    inheritance graph and their data members. If that were not anticipated
    throughout the entire graph it could result in leaks and hard to find bugs.
    Is that why?

    The constructor scenario aside, suppose that I want to augment a regular
    non-pure virtual method that I am overriding rather than replace it
    wholesale. Isn't it perfectly fine to use explicit qualification to supress
    the virtual call mechanism in order to invoke base class method
    implementation and then add my other additional logic?

    -Bern McCarty

    "Doug Harrison [MVP]" <dsh@mvps.org > wrote in message
    news:l0if209cgf k7km3cr1aaos8r6 aab5p04rp@4ax.c om...[color=blue]
    > Peter E. Granger wrote:
    >[color=green]
    > >[I see there have been some other replies to this message in the interim;
    > >probably by now somebody else has hit on the same solution.]
    > >
    > >With the help of an ingenious friend (thank you, Theo!), I was able to[/color][/color]
    find[color=blue][color=green]
    > >out how to make the constructor calls that I wanted. It's kind of an
    > >odd-looking construct, and since no one here knew about it, I guess it's[/color][/color]
    not[color=blue][color=green]
    > >something that's commonly done in C++.
    > >
    > >On the off-chance that it'll be useful to someone else, here's the code:
    > >
    > >Sphere::Sphere (int x, int y, int z, int r)
    > >{
    > > //Sphere(x, y, z); // Wrong! Just makes and destroys temp object
    > > this->Sphere::Sphere (x, y, z); // Right! Initializes all members.
    > > this->Radius = r;
    > >}[/color]
    >
    > The above is bogus, both from a Standard C++ point of view and[/color]
    conceptually,[color=blue]
    > because even if it were legal, it would be equivalent to using placement[/color]
    new[color=blue]
    > on a partially constructed object and would be wrong for all the reasons
    > that method is wrong. It's a non-standard leftover from the dark ages of
    > VC++. Don't use it. You've been given correct answers by myself and others
    > in this thread.
    >
    > --
    > Doug Harrison
    > Microsoft MVP - Visual C++[/color]


    Comment

    • Doug Harrison [MVP]

      #32
      Re: Calling one constructor from another in VC++

      Bern McCarty wrote:
      [color=blue]
      >Hmmmm. Why do you say that explicit qualification is bogus/illegal from a
      >Standard C++ point of view?[/color]

      I didn't say that.
      [color=blue]
      >I can buy that using it to call another
      >constructor on a partially constructed object might be a mistake, but I
      >question the "even it it were legal" and "its non-standard" assertions.
      >Explicit qualification appears to be perfectly legal and standard C++.[/color]

      You can't directly call a constructor. Thus the following is illegal:
      [color=blue][color=green][color=darkred]
      >>>Sphere::Sphe re(int x, int y, int z, int r)
      >>>{
      >>> //Sphere(x, y, z); // Wrong! Just makes and destroys temp object
      >>> this->Sphere::Sphere (x, y, z); // Right! Initializes all members.
      >>> this->Radius = r;
      >>>}[/color][/color][/color]
      [color=blue]
      >Is it not? From Section 10.2 "Member name lookup" of the C++ standard[/color]

      See 12.1/2.
      [color=blue]
      >And there are other places in the standard where explicit qualification is
      >used to make various points. I easily believe that using explicit
      >qualificatio n to call one constructor from another is bad, but I would like
      >to better understand why. My own guess is that it would be bad because it
      >could cause double invocation of constructors for all nodes of the
      >inheritance graph and their data members. If that were not anticipated
      >throughout the entire graph it could result in leaks and hard to find bugs.
      >Is that why?[/color]

      That's certainly one very big reason. This being C++, there may be some more
      obscure ones I haven't thought of. :)
      [color=blue]
      >The constructor scenario aside, suppose that I want to augment a regular
      >non-pure virtual method that I am overriding rather than replace it
      >wholesale. Isn't it perfectly fine to use explicit qualification to supress
      >the virtual call mechanism in order to invoke base class method
      >implementati on and then add my other additional logic?[/color]

      Yep.

      --
      Doug Harrison
      Microsoft MVP - Visual C++

      Comment

      Working...