Declare a C++ class without defining it in the current translation unit -


it possible declare class without defining (forward declaration) long defined later on within translation unit. in case of functions, 1 can declare function without defining within translation unit, , linker link definition in different translation unit. possible same class declarations?

(if not possible, there any use forwardly declared class without definition in current tl, or error?)

something this, except doesn't compile:

mymain.cpp:

class myclass; // declare without defining  myclass::myclass(); void myclass::barf();  int main() {   myclass *m = new myclass();   m->barf();   return 0; } 

myclass.cpp:

#include <iostream>  class myclass { // define implementation public:     myclass();     void barf(); };  myclass::myclass() { } //empty constructor void myclass::barf() {     std::cout << "barfing\n"; } 

it possible forward-declare class, pointers , references forward-declared classes can used. can't use actual object of forward-declared class because compiler doesn't know enough it; in particular doesn't know how large object or members are. trying forward-declare member functions (as have done) won't work because syntax of forward declaration doesn't allow specify whether functions virtual or non-virtual (or perhaps inherited base class).

it not useful forward-declare class in source file, can useful in header file. in particular it's common forward-declare class in library's public header file , use pointers type opaque handles. class definition remains private library user code can pass around pointers objects of class without ever knowing class's implementation looks like. works particularly when pointers smart pointers.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -