Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » c++ nested class protyping

This thread is locked; no one can reply to it. rss feed Print
c++ nested class protyping
oscar_spencer
Member #8,914
August 2007

How does one prototype a nested class?

for example with this in one file

class Outer
{
public:
  class Inner
  {
    ...
  };
  ...
};

i prototype Outer in another file.

class Outer;

but how do i prototype Inner?
Ive tried

class Outer::Inner;

but it gives me errors when i declare a pointer of type Inner later

Outer::Inner * variable;

Any help much appreciated.
--scott

Roy Underthump
Member #10,398
November 2008
avatar

http://www.allegro.cc/forums/thread/594939

I'm not sure if it's quite the same problem

I love America -- I love the rights we used to have
George Carlin

oscar_spencer
Member #8,914
August 2007

thanks, but not quite the same problem
AFAIK namespaces allow multiple entries in a file
like this is legal

namespace ANamespace
{
  ...definitions
}
...other code
namespace ANamespace
{
  ...definitions
}

this is not legal

class AClass
{
  ...definitions
};
...other code
class AClass
{
  ...definitions
};

anonymous
Member #8025
November 2006

You might just make Inner a non-nested normal class. If it is inner for name control only (so that the name of Outer would be part of the class name), perhaps a namespace could be used instead.

As far as I know, there isn't anything really special about nested classes, except perhaps conveying the meaning that they should only be used by the enclosing class (when declared private), so that there are no objective reasons that would force you to use one or another.

Thomas Fjellstrom
Member #476
June 2000
avatar

I had the same issue with a "little" C++ lib I was working on. Make the inner class a regular class with a funny name, and typedef it into the outer class if you still want to access it inside like you did before.

but to be brutally honest, nested classes are useless in C++. They really don't provide anything usefull over a regular class.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

but to be brutally honest, nested classes are useless in C++. They really don't provide anything usefull over a regular class.

It does make it so the nested class can only be used within the proper scope of the container class. If you have a nested class inside a protected/private section, nothing outside of the container class (and maybe it's derivitives) that isn't specificly friended can use it.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

ReyBrujo
Moderator
January 2001
avatar

Does that work? Never tried nested classes, makes everything harder to read in headers.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Paul whoknows
Member #5,081
September 2004
avatar

1class CMyClass
2{
3 private:
4
5 public:
6 CMyClass();
7
8 class CMyOtherClass
9 {
10 private:
11 public:
12 CMyOtherClass();
13
14 };
15
16}
17 
18CMyClass::CMyClass()
19{
20 stuff here
21}
22 
23CMyClass::CMyOtherClass::CMyOtherClass()
24{
25 stuff here
26}
27 
28//Hope you get the idea.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

ReyBrujo
Moderator
January 2001
avatar

Yuck, awful. Looks like nested namespaces.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

anonymous
Member #8025
November 2006

The nested class should be in the private/protected section of the outer class. Then it is available only for the containing class and you'll get a compiler error should you try to use the nested class elsewhere ("class X is private in this context").

Of course, you could also use separate classes and make everything in the "nested" class private and give access to the "outer" class through friendship, only that gives "outer" access to everything, not just its public interface.

Paul whoknows
Member #5,081
September 2004
avatar

Quote:

Yuck, awful.

I find inner classes useful when I have to implement relatively complex tasks in the methods of the outer class.
If you need classes for your program, I can't see why you shouldn't need classes for your methods?

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

I find inner classes useful when I have to implement relatively complex tasks in the methods of the outer class.

But they really don't need to be inner classes then, just a small helper class, doesn't really matter where you put it.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

oscar_spencer
Member #8,914
August 2007

Thanks for all the replies,

seems the general consensus is to not use nested classes
unless theyre private.
for my use it should be possible to use a helper class
instead.

I think that it makes the interface cleaner tho, especially
like in the STL.

list<Object>::iterator it;

anonymous
Member #8025
November 2006

Don't forget about the typedefs. I'm pretty sure that in your STL implementation you'll find that the iterators are not nested types and the "nestedness" comes from typedefs like this:

      typedef _List_iterator<_Tp>                        iterator;
      typedef _List_const_iterator<_Tp>                  const_iterator;
      typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
      typedef std::reverse_iterator<iterator>            reverse_iterator;

As you can see, the STL already contains an implementation of std::reverse_iterator that can be used to synthesize reverse iterators from any bidirectional iterator, so at least this couldn't be a nested class.

Go to: