I'm not sure I understand the question. If the question is "how do I call a constructor without using the keyword new
?" the answer is:
vfunc vf;
If the question is "I already have a vfunc
object, and want to clear it out, how do I call its constructor?" (like the title implies), the answer is that you don't call the constructor again, but instead write a different function called something like clear()
so that clear()
has the effect of clearing the object's state.
However, I think the question is actually "I have three classes that rely on each other, I can't figure out a good order to define the classes so that I have definitions for class vfunc
when I'm defining class Commands
and have definitions for class Commands
when I'm defining class vfunc
.
The answer to that is "you can split the definition and the declaration of a class, this is the main reason for header files (put the declaration in the header and the definition in a separate file). But with a mutual dependency like in your question, you'll also need to forward declare either Commands
or vfunc
before you fill out the methods and members of the other. If you forward declare, then you'll have to change the function signature to take either a reference or a pointer:
class Commands; // forward declaration
// at this point, the compiler knows that a class Commands
// will eventually be defined, but without more information, the
// compiler can only handle Commands& and Commands*
// (i.e., references and pointers)
class vfunc {
vfunc() { ... }
vfunc(const Commands& command);
// since the compiler still doesn't know anything *about* Commands,
// like what member variables or member functions it has,
// or even what to return for sizeof(Commands),
// you really aren't going to be able to define this constructor here;
// once you've declared the necessary information, you can then define
// this function (either in this file or in another file, which #includes
// this file in order to know about the internals of Commands
...
};
class Commands { ... };
// define that constructor, this can be done in a separate file
vfunc::vfunc(const Commands& command) { ... }
Forward declarations have lots of uses, most notably reducing compile times (because fewer header files need to be loaded), but also to implement the PIMPL idiom and for efficient platform dependent code (the APR does this last one).