473,224 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

nested class

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
4 1465
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
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
>>
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.