Topic: easy question: constructor syntax


Author: Chris <cmrchs@gmail.com>
Date: Mon, 29 Nov 2010 13:14:19 CST
Raw View
Hello.

suppose:

  class X  {
   // ...
  };

  X  obj1;
  X  obj2();

obj1 represents an object of type X.

But what does obj2 represent and what can you do with it?

thank you
Chris


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: schep <aschepler@gmail.com>
Date: Mon, 29 Nov 2010 17:21:27 CST
Raw View
On Nov 29, 2:14 pm, Chris <cmr...@gmail.com> wrote:
>   X  obj1;
>   X  obj2();
>
> obj1 represents an object of type X.
>
> But what does obj2 represent and what can you do with it?

The "X obj2();" is a function declaration.  It says that obj2 is a
function which takes no parameters and has return type X.

You can define the function like "X obj2() { return X(); }", or call
it like "X obj3 = obj2();".


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Mon, 29 Nov 2010 17:22:01 CST
Raw View
On 29/11/2010 19:14, Chris wrote:

>
> Hello.
>
> suppose:
>
>   class X  {
>    // ...
>   };
>
>   X  obj1;
>   X  obj2();
>
> obj1 represents an object of type X.
>
> But what does obj2 represent and what can you do with it?
>
> A function called obj2, taking no arguments and returning an X by value.
This unfortunate parse is one of the reasons that I, among others, fought
hard to have a new initialisation syntax in C++0x.

You can now write:

X obj2{};

and get a default/value initialised object of type X. At least you will be
able to on any implementation that supports the new C++0x initialisation
syntax.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Chris <cmrchs@gmail.com>
Date: Tue, 30 Nov 2010 11:35:33 CST
Raw View
On Nov 30, 12:22 am, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
> On 29/11/2010 19:14, Chris wrote:
>
>
>
>
>
> > Hello.
>
> > suppose:
>
> >   class X  {
> >    // ...
> >   };
>
> >   X  obj1;
> >   X  obj2();
>
> > obj1 represents an object of type X.
>
> > But what does obj2 represent and what can you do with it?
>
> > A function called obj2, taking no arguments and returning an X by value.
>
> This unfortunate parse is one of the reasons that I, among others, fought
> hard to have a new initialisation syntax in C++0x.
>
> You can now write:
>
> X obj2{};
>
> and get a default/value initialised object of type X. At least you will be
> able to on any implementation that supports the new C++0x initialisation
> syntax.
>
> --
> [ comp.std.c++ is moderated.  To submit articles, try posting with your ]
> [ newsreader.  If that fails, use mailto:std-cpp-sub...@vandevoorde.com ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]

thank you!

Chris


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]