Thread: Forward Declaration of Class Members?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Forward Declaration of Class Members?

    Hi,
    I am working on a simple particle system, and am having a problem involving emitters.

    The particle engine works roughly as follows:
    Code:
    class Emitter;
    
    class ParticleEngine {
        void update() {
            for (each Emitter i) {
                i->emit (this);
            }
        }
    
        void addParticle () {
            //...
        }
    }
    
    class Emitter {
        void emit (ParticleEngine* pe) {
            pe->addParticle ();
        }
    }
    The issue here is that both classes must be able to call methods on the other class. Is there any way to do a forward declaration of a class member? The alternative would be to have emit return a list of particles to add, but this seems simpler.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Implement the functions in a source file (or two). Then, you will likely only need forward declarations in the header(s).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This looks like Java, not C++:
    Code:
    for (each Emitter i) {
    If you forward declare a class, you can only refer to a pointer to it, so you'll need to use header files & .cpp files like Daved suggested.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    This looks like Java, not C++:
    Java doesn't have the -> operator. I think it's just a bit of pseudocode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  3. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  5. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM