forward declarations

D

Dylan

If I have a class with a nested struct such as

class MyClass
{
class NestedClass
{};
};

Is it possible to forward declare the nested class in a separate
header file? I want to do something like the following but my compiler
keeps slapping my wrists!

#ifndef SOME_HEADER_H
#define SOME_HEADER_H

class MyClass::NestedClass;

class AnotherClass
{

MyClass::NestedClass* m_pNestedClass;

...
};

#endif

thanks for any help
 
B

benben

Is it possible to forward declare the nested class in a separate
header file? I want to do something like the following but my compiler
keeps slapping my wrists!

As far as I know, nope. The nested class is part of the interface of the
(non-nested) class and so cannot be completely lifted from the class. You
can, however, forward declare the nested class withing the outer class and
then somewhere still within the outer class define the nested class.

Ben
 
V

Victor Bazarov

benben said:
As far as I know, nope. The nested class is part of the interface of the
(non-nested) class and so cannot be completely lifted from the class. You
can, however, forward declare the nested class withing the outer class and
then somewhere still within the outer class define the nested class.

There is no need to do it "still within the outer class". This:

class Outer {
class Inner; // declaration
};


class Outer::Inner { // definition
int abc;
};

is perfectly fine.

V
 

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,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top