Topic: illegal constructor syntax???


Author: Woody Rowand <wrowand@adc.com>
Date: 1997/12/19
Raw View
I've come across the following constructor call syntax and although it
clearly is a problem, I couldn't find anything in the ARM that forbid
it.  I was wondering if the standard mentioned it.

   class MyClass {
   public:
      MyClass();
      MyClass(int x);
   };

   void foo() {
      MyClass x;

      x.MyClass(100);     <---- this is the strange ctor call
   }


My compiler (g++) doesn't complain about this.  When I tested this, I
found that "x.MyClass(100)" does indeed call the constructor with no
change in the "this" pointer -- effectively reconstructing the object,
although without destroying the previously constructed object (i.e.,
the one that was constructed with the default ctor).

Thanks,

/Woody

--
Woody Rowand
ADC Telecommunications - TSG         internet: wrowand@adc.com
2240 Campbell Creek Blvd.               voice: 972.680.7623
Richardson, TX 75082                      fax: 972.680.7672
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/22
Raw View
Woody Rowand wrote:
>
> I've come across the following constructor call syntax and although it
> clearly is a problem, I couldn't find anything in the ARM that forbid
> it.  I was wondering if the standard mentioned it.
>
>    class MyClass {
>    public:
>       MyClass();
>       MyClass(int x);
>    };
>
>    void foo() {
>       MyClass x;
>
>       x.MyClass(100);     <---- this is the strange ctor call
>    }
>
> My compiler (g++) doesn't complain about this.  When I tested this, I
> found that "x.MyClass(100)" does indeed call the constructor with no
> change in the "this" pointer -- effectively reconstructing the object,
> although without destroying the previously constructed object (i.e.,
> the one that was constructed with the default ctor).

As far as I know, it's not allowed in the standard, so it must be an extension
(of which G++ seems to have a few). The correct way to apply a constructor is
with the placement new syntax:

 new(&x) MyClass(100);

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]