Topic: unnamed classes / structs without derivation?


Author: Daniel Frey <daniel.frey@aixigo.de>
Date: Mon, 3 Jun 2002 14:48:45 GMT
Raw View
Allan W wrote:
>=20
> Daniel Frey <daniel.frey@aixigo.de> wrote in message news:<3CF4C795.288=
B7546@aixigo.de>...
> > James Kanze wrote:
> > >
> > > Daniel Frey <daniel.frey@aixigo.de> writes:
> > >
> > > |>  a question about unnamed classes / structs:
> >
> > > |>  The following is legal:
> >
> > > |>  class {
> > > |>    int x;
> > > |>  } y;
> > >
> > > Legal, but not very useful -- there is no way anyone can access x.
> >
> > Well, I have a use for it, but it is really a strange thing :) If you
> > like, I can send a small example, but I don't think it's appropriate =
for
> > csc++. Drop a private mail if you are interested.
>=20
> Why not just change "class" to "struct"? Then you can access it.

Hm, I received several private email to show an example. I hope you (and
the others) didn't focused too much on the class itself, as it _is_
pretty useless. The main point of my posting was the unnamed class.
Given that, you can access private/protected data and functions through
the derivation of a base class which - of course - has a public
function. The example:

#include <iostream>
#include <list>
using namespace std;

class init_function
{
private:
   typedef std::list< init_function* > L;
   static L l_;

protected:
   init_function()=20
   {
      l_.push_back( this );
   }

public:
   static void init_all()
   {
      for( L::iterator it =3D l_.begin(); it !=3D l_.end(); ++it ) {
         (*it)->init();
      }

      l_.clear();
   }
  =20
protected:
   virtual void init() =3D 0;
};

init_function::L init_function::l_;

class Dummy1 : init_function
{
   virtual void init()
   {
      cout << "Init 1" << endl;
   }
} dummy1;

class Dummy2 : init_function
{
   virtual void init()=20
   {
      cout << "Init 2" << endl;
   }
} dummy2;

int main()
{
   init_function::init_all();
  =20
   cout << "Hello, World!" << endl;
}

Imagine that classes Dummy1 and Dummy2 are in different shared
libraries. I can now decide to link them to a program and they register
their init-functions automatically. In my program's main()-function (or
somewhere else), I can execute all registered init-functions at a
certain defined time and after some basic setups/inits. The names of the
classes (Dummy1 and Dummy2) are not needed, these could be unnamed
classes; but due to a bug in the gcc, I have to specify them.

Actually, even the identifiers for the variable are superfluous. So...
how about unnamed variables? Probably:

class ... { ... } ();

No... just kidding... :)

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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: Allan_W@my-dejanews.com (Allan W)
Date: Thu, 30 May 2002 21:51:42 GMT
Raw View
Daniel Frey <daniel.frey@aixigo.de> wrote in message news:<3CF4C795.288B7546@aixigo.de>...
> James Kanze wrote:
> >
> > Daniel Frey <daniel.frey@aixigo.de> writes:
> >
> > |>  a question about unnamed classes / structs:
>
> > |>  The following is legal:
>
> > |>  class {
> > |>    int x;
> > |>  } y;
> >
> > Legal, but not very useful -- there is no way anyone can access x.
>
> Well, I have a use for it, but it is really a strange thing :) If you
> like, I can send a small example, but I don't think it's appropriate for
> csc++. Drop a private mail if you are interested.

Why not just change "class" to "struct"? Then you can access it.

---
[ 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: Daniel Frey <daniel.frey@aixigo.de>
Date: Tue, 28 May 2002 21:32:14 GMT
Raw View
Hi,

a question about unnamed classes / structs:

The following is legal:

class {
  int x;
} y;

But AFAIK this isn't:

class Dummy {};

class : public Dummy
{
   int x;
} y;

Any reason why? The latter is a shortened example of the only use for
unnamed classes I found so far, so I wonder why it's illegal. Or is my
compiler (gcc 2.95.2) wrong complaining about it?

Regards, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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: James Kanze <kanze@alex.gabi-soft.de>
Date: Wed, 29 May 2002 06:58:34 CST
Raw View
Daniel Frey <daniel.frey@aixigo.de> writes:

|>  a question about unnamed classes / structs:

|>  The following is legal:

|>  class {
|>    int x;
|>  } y;

Legal, but not very useful -- there is no way anyone can access x.

A good compiler will generate a warning.

|>  But AFAIK this isn't:

|>  class Dummy {};

|>  class : public Dummy
|>  {
|>     int x;
|>  } y;

Why not?  It suffers the same problem as the preceding, but other than
that, it seems perfectly legal to me.

|>  Any reason why? The latter is a shortened example of the only use
|>  for unnamed classes I found so far, so I wonder why it's
|>  illegal. Or is my compiler (gcc 2.95.2) wrong complaining about
|>  it?

G++ (both 2.95.2 and 3.0.4) complains with message about a parse
error.  But the grammar is clear in the standard: the identifier in a
class-head is optional.  If it wasn't, the : wouldn't be necessary.

--
James Kanze                                mailto:kanze@gabi-soft.de
Need real expertise in C++, Java, OO design
                       I am available, see my CV at www.gabi-soft.de
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(0)69 63198627

---
[ 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: Daniel Frey <daniel.frey@aixigo.de>
Date: Wed, 29 May 2002 19:33:39 GMT
Raw View
James Kanze wrote:
>=20
> Daniel Frey <daniel.frey@aixigo.de> writes:
>=20
> |>  a question about unnamed classes / structs:
>=20
> |>  The following is legal:
>=20
> |>  class {
> |>    int x;
> |>  } y;
>=20
> Legal, but not very useful -- there is no way anyone can access x.

Well, I have a use for it, but it is really a strange thing :) If you
like, I can send a small example, but I don't think it's appropriate for
csc++. Drop a private mail if you are interested.

> A good compiler will generate a warning.
>=20
> |>  But AFAIK this isn't:
>=20
> |>  class Dummy {};
>=20
> |>  class : public Dummy
> |>  {
> |>     int x;
> |>  } y;
>=20
> Why not?  It suffers the same problem as the preceding, but other than
> that, it seems perfectly legal to me.

OK. I also received a private mail that suggests that the standard
allows it but gcc is wrong, thus I will send a bug-report to the
gcc-folks as soon as I found out about the current status in gcc 2.95.3
/ 2.95.4 / 3.1 / 3.2.=20

Thanks, Daniel

--
Daniel Frey

aixigo AG - financial training, research and technology
Schlo=DF-Rahe-Stra=DFe 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

---
[ 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                       ]