Skip to main content
GameDev.net gamedev.net
🔒 Locked

Declaring nested classes without defining them

Started by spraff Feb 23, 2009 at 9:52 AM 0 replies 550+ views
Original Post
spraff
spraff
I'm trying to define nested classes with this basic structure:

class Outer {
public:
	class Inner;	// OK
	class Inner::X;	// Not OK
private:
	friend class Inner::X;
	// Stuff
};

class Outer::Inner {
public:
	class X {
		// Stuff
	};
	// Stuff
};

How can I make Outer aware of the declaration of Inner::X without first defining Inner? (The point of this design, by the way, is that Inner::X grants finer-grained access to Outer than C++ natively allows. For the sake of namespace tidiness, I am loathe to make Inner::X a top-level class so I can forward-declare it. Is what I'm trying to do actually impossible?
visitor
visitor
If this is C++ then basically I don't think you can do that. Inner is an incomplete type, hence you can't forward declare anything in it.

All in all, nested classes don't have any magic properties. You can always use normal "free" classes. To hide them, perhaps you can put them into a namespace whose name indicates that these contents shouldn't be used by end user (detail, impl_detail etc), or perhaps, if they are only to be used by the Outer class you just declare them in the implementation file.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.