C++ Define Class in One File and Forward Declare It in Another

Is it legal/advised to do this in C++

//Interface.h
#ifndef INTERFACE_H
#define INTERFACE_H
    #include "WinImplementation.h"
    #include "NixImplementation.h"
    class Interface {
        class WinImplementation;
        class NixImplementation;
    }
#endif

//WinImplementation.h
#ifndef WINIMPLEMENTATION_H
#define WINIMPLEMENTATION_H
    #include "Interface.h"
    class Interface::WinImplementation {}
#endif

//NixImplementation.h
#ifndef NIXIMPLEMENTATION_H
#define NIXIMPLEMENTATION_H
    #include "Interface.h"
    class Interface::NixImplementation {}
#endif

Answers


Yes, you can forward declare nested classes in C++. The following example is taken directly from the C++ standard (section 9.7.3):

class E
{
    class I1;     // forward declaration of nested class
    class I2;
    class I1 {};  // definition of nested class
};
class E::I2 {};   // definition of nested class

Need Your Help

Quartz scheduler Not Working after exception

java exception weblogic scheduled-tasks quartz-scheduler

I am currently working with Quartz scheduler in my web application to schedule a task after running the server and then repeat it in regular interval.

Retrieve user playlist using YouTube API

asp.net .net youtube youtube-api

I have this code at the moment that authenticates a user and tries to retrieve the current authenticated users playlist but the request is inavlid and the error is as so

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.