Topic: Unnamed Classes


Author: AllanW@my-dejanews.com
Date: 1998/11/20
Raw View
In article <72sh6f$fi8$1@newsmonger.rutgers.edu>,
  "Ajay Wanchoo" <wanchoo@caip.rutgers.edu> wrote:
>
> Hi,
> Pl. consider the following (also pl. note that I'm not an expert):
>
> 1. One can't specify member functions and more importantly any ctor/dtor
> for unnamed classes. It *might* be useful to have a mechanism to allow
> this (especially for small/trivial classes that have only one instance in
> the entire program). e.g.
[reformatted]
>     class{
>         int a;    // I might want to initialize the integer a,
>         char *b;  // and allot some memory to b
>     } Alpha;

Then don't use an unnamed class.
     class AlphaClass {
         int a;    // I might want to initialize the integer a,
         char *b;  // and allot some memory to b
     public:
         AlphaClass() : a(10), b(new char[10]) {}
         ~AlphaClass() { delete[] b; }
     } Alpha;

Also, note that your version didn't have any access specifiers.
Therefore the members were private; not a very useful class.
The fix would be to change the keyword "class" to the keyword
"struct", which does exactly the same thing except that it
defaults to public access. Furthermore, the keyword "struct"
might give readers a better idea what you're really doing with
the class, which is treating it as a structure (possibly with
some neet-o initialization and/or cleanup).

> 2. Maybe an identifier like "this" (pointer to self object) i.e. a ref to
> the class
> like a "thisclass" identifier would help to resolve which class is being
> referred to. It may even help in regular coding, even though you could get
> the class info using the typeid operator.

That's rarely needed, but when it is you can use the class name
followed by two colens, as in:
    class MyClass : public YourClass {
    public:
        int myInt;
        void myFunc() { MyClass::myInt = YourClass::myInt + 2; }
    }

> 3. Further, even inheritance is not allowed, which is sad but that's another
> story.

Give the class a name, as shown above. Then inheritance is fine.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Ajay Wanchoo" <wanchoo@caip.rutgers.edu>
Date: 1998/11/17
Raw View
Hi,
Pl. consider the following (also pl. note that I'm not an expert):

1. One can't specify member functions and more importantly any ctor/dtor
for unnamed classes. It *might* be useful to have a mechanism to allow
this (especially for small/trivial classes that have only one instance in
the
entire program).
e.g.
class{
 int a;
 char *b;
}Alpha;// I might want to initialize the integer a, and allot some memory to
b

2. Maybe an identifier like "this" (pointer to self object) i.e. a ref to
the class
like a "thisclass" identifier would help to resolve which class is being
referred to. It may even help in regular coding, even though you could get
the class info using the typeid operator.

3. Further, even inheritance is not allowed, which is sad but that's another
story.

Ajay




[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/18
Raw View
Ajay Wanchoo wrote:
>
> Hi,
> Pl. consider the following (also pl. note that I'm not an expert):
>
> 1. One can't specify member functions and more importantly any ctor/dtor
> for unnamed classes. It *might* be useful to have a mechanism to allow
> this (especially for small/trivial classes that have only one instance in
> the
> entire program).
> e.g.
> class{
>  int a;
>  char *b;
> }Alpha;// I might want to initialize the integer a, and allot some memory to
> b
>
> 2. Maybe an identifier like "this" (pointer to self object) i.e. a ref to
> the class
> like a "thisclass" identifier would help to resolve which class is being
> referred to. It may even help in regular coding, even though you could get
> the class info using the typeid operator.
>
> 3. Further, even inheritance is not allowed, which is sad but that's another
> story.

How would you call the constructor for an object of an unnamed class?
How would you identify the base class of a derived class, when it's
unnamed? Is naming the class such a difficult solution?
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]