nested class

J

James Brown

Hi,

I have the following:

class outer
{
public:
outer();

private:

class inner
{
public:
inner();
};
};


In other words, a nested class. In my particular case, the inner class
definition is quite large and I'd like to define it outside of the outer
class, but still have it remain as an 'inner' class. The idea being, to keep
the code of 'outer' tidy but still have the inner class as a private class
to 'outer'.

e.g.

class outer
{
.....

// forward-declare 'inner'
class inner;
};

// define 'inner' separately from 'outer'
class outer::inner
{
.....
};

I'm getting compiler errors of course, so I'm guessing that this kind of
syntax isn't possible. What I would like to know however, is what is the
'best practice' for dealing with this kind of thing? Do I have to shrug my
shoulders and have a monolithic outer class with everything defined within
it? or is there some kind of neat design I can follow somewhere..?

thanks,
James
 
M

mast2as

you can always do that

class TT {
public:
TT() {}
~TT() {}
private:
class TTP {
public:
TTP();
~TTP();
};
};


TT::TTP::TTP() { cout << "constructor" << endl; }
TT::TTP::~TTP() { cout << "destructor" << endl; }
 
A

Andrey Tarasevich

James said:
...
In other words, a nested class. In my particular case, the inner class
definition is quite large and I'd like to define it outside of the outer
class, but still have it remain as an 'inner' class. The idea being, to keep
the code of 'outer' tidy but still have the inner class as a private class
to 'outer'.

e.g.

class outer
{
.....

// forward-declare 'inner'
class inner;
};

// define 'inner' separately from 'outer'
class outer::inner
{
.....
};

I'm getting compiler errors of course, so I'm guessing that this kind of
syntax isn't possible.

It is a valid syntax in C++. Apparently, there's something wrong with
your compiler.
What I would like to know however, is what is the
'best practice' for dealing with this kind of thing?

If changing the compiler is not an option, then I don't even know what
to suggest. If the compiler's support for nested classes is limited as
illustrated by the above example, then there's a real risk that there
are other issues with it as well, and you will run into them sooner or
later. Maybe with this particular compiler it is a good idea to avoid
nested classes altogether.
 
J

James Brown

It is a valid syntax in C++. Apparently, there's something wrong with
your compiler.

I am admitedly using an old compiler - upgrading is not a problem so that's
my solution.

Thanks,
James
 
E

Earl Purple

James said:
I am admitedly using an old compiler - upgrading is not a problem so that's
my solution.

Thanks,
James

Your other option would be not to use nested classes. You can then
typedef inner into outer if it needs to have that name for template
use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top