Zilarion
Zilarion

Reputation: 302

Extending a class in C++ using header files

I found found out that to extend a class I would have to do this:

class x : public extendsThis { // rest here
};

So, I create 2 classes, in two seperate files:

- particle.h -
class particle : public program {
};

 - program.h -

 class program {
    particle* myParticle;
 };

However, in the program class I wanted to have particle objects, which then again extend the program class.

This would however require me to have a include program.h in the particle class, and a particle.h include in the program class to let them know about each other. This however created some infinite loop that made me unable to compile.

Is there any way around this, or am I doing something wrong?

Upvotes: 2

Views: 6667

Answers (5)

Chris Frederick
Chris Frederick

Reputation: 5584

I am posting this answer to explain what I meant by the dangers of calling the default particle constructor from the default program constructor.

#include <iostream>

class particle;

class program {
    particle* myParticle;

public:
    program();
};

class particle : public program {
public:
    particle()
    {
        std::cout << "Particle constructor\n" << std::flush;
    }
};

program::program()
{
    std::cout << "Program constructor\n" << std::flush;
    myParticle = new particle();
}

int main()
{
    program *myProgram = new program();
    delete myProgram;
    return 0;
}

When I compile and run this program, it continues to print "Program constructor" because particle::particle() always calls program::program() first, resulting in an infinite loop. Commenting out the line myParticle = new particle(); fixes this problem.

Upvotes: 0

Bryce Siedschlaw
Bryce Siedschlaw

Reputation: 4226

It may just be me, but it seems like you're doing this wrong. Why would you want to extend the particle class with the program class? It theory you'd get something like this:

class particle {
    particle *myParticle;
};

Notice how extending the class could potentially create an infinite loop? I mean, this is just my understanding and I could be horribly wrong, but it's something to consider. Someone let me know if I'm waaaay off here.

Upvotes: 0

David V
David V

Reputation: 11699

Yes, you can work around this. You can make a very simple class declaration.

In program.h:

class particle;

class program
{
  particle *myParticle;
};

Keep in mind that you need to use a pointer to particle.

Upvotes: 3

Jay
Jay

Reputation: 14471

In some cases you can use a forward declaration in one of the include files:

class a;

class b { a* ptr; };

It can also be an indication your design may need to be re-thought. Generally your program should "know" about it's components but not the reverse. The components of the program should be small stand alone classes with a simple well defined function. If you entangle all the classes you get "spooky action at a distance" (bugs). ;)

Upvotes: 1

Chris Morgan
Chris Morgan

Reputation: 2080

Use include guards in your header files: http://en.wikipedia.org/wiki/Include_guard

Upvotes: -2

Related Questions