Forward Declaration of Class Members?

This is a discussion on Forward Declaration of Class Members? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am working on a simple particle system, and am having a problem involving emitters. The particle engine works ...

  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,317
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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, 02:49 PM
  5. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21