By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

nested class

P: n/a
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
Jul 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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; }

Jul 5 '06 #2

P: n/a
James Brown wrote:
...
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.

--
Best regards,
Andrey Tarasevich
Jul 6 '06 #3

P: n/a
>>
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.
I am admitedly using an old compiler - upgrading is not a problem so that's
my solution.

Thanks,
James


Jul 6 '06 #4

P: n/a

James Brown wrote:
>
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.

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.

Jul 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.