Topic: unnamed class constructor


Author: Elias Pouloyiannis <stud1150@di.uoa.gr>
Date: Wed, 31 Mar 2004 15:30:08 +0000 (UTC)
Raw View
consider this example:

#include <iostream>
using namespace std;

class {
         public:
                 int getX()
                 {
                         return _x;
                 }
                 void setX( int x )
                 {
                         _x = x;
                 }
         private:
                 int _x;
} global;

int main (){
         global.setX(10);
         cout << global.getX() << endl;
         return 0;
}
/********************/

In the above code the variable global is well... a global variable of an
unnamed type.
However not knowing it's name i am available of creating a constructor for
it (not one that initialises it's members instead of assigning values to
them i.e. named constructor);

How can one create a constructor for an unnamed class?
I'd really like an answer on this.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]




Author: kuyper@wizard.net (James Kuyper)
Date: Fri, 2 Apr 2004 16:50:03 +0000 (UTC)
Raw View
Elias Pouloyiannis <stud1150@di.uoa.gr> wrote in message news:<c3va8s$ac7$1@usenet.otenet.gr>...
..
> How can one create a constructor for an unnamed class?
> I'd really like an answer on this.

You can't. If the default constructor is unacceptable, you're going to
have to name the class in order to provide non-default semantics for
the construction of the objects.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: stud1150@di.uoa.gr (Elias Pouloyiannis)
Date: Mon, 15 Mar 2004 18:38:53 +0000 (UTC)
Raw View
How should i declare the constructor for an unnamed class?
consider the following example

#include <iostream>
using namespace std;

class {
=A0=A0public:
=A0=A0=A0=A0int=A0getX(){=A0return=A0_x;=A0}
=A0=A0private:
=A0=A0=A0=A0int=A0_x;
} global;

int main (){
=A0=A0cout=A0<<=A0global.getX()=A0<<=A0endl;
}

How should i initialise the privete member _x of the class?
Is the named constructor idiom my only choice here?
And don't ask why on earth whould i ever create a nameless class... i am
just wondering ;)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]