I’ve got a problem in a C++ program that I can’t solve.
I’m trying to pass an object as a construction parameter of an other but i can’t figure out how to do this.
My main is creating a Simulation object. In simulation i’m creating two objects a planete and a modelisation. The thing is, i want to pass the planete object in the modelisation object.
My first object is Simulation.
#include "Simulation.h"
#include "Modelisation.h"
#include "Planete.h"
Planete *Obj;
Modelisation *Model;
Simulation::Simulation (int argc, char **argv)
{
Obj = new Planete ("Terre", (5.98*pow(10.0,24.0)), 6378.137, 0, 0, 0);
Model = new Modelisation (argc, argv,"Modelisation",1600 ,1004, 306, 0);
};
Simulation::~Simulation ()
{
delete Obj;
delete Model;
};
Modelisation.h
class Modelisation
{
private:
int hauteur, largeur, x, y;
int fenetre;
//Planete ClonePlanete
public:
Modelisation (int argc, char **argv, char[], int, int, int, int);
~Modelisation ();
static void Dessiner ();
static void Redessiner (int, int);
void InitCallBack ();
};
Modelisation.cpp
#include "stdafx.h"
#include "Modelisation.h"
#include "Camera.h"
#include "Planete.h"
Camera *camera;
Modelisation::Modelisation (int argc, char **argv, char nomFenetre [] ,int hauteur, int largeur, int x, int y)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (hauteur, largeur);
glutInitWindowPosition (x, y);
fenetre = glutCreateWindow (nomFenetre);
camera = new Camera;
// Planete ClonePlanete = new Planete(planete); // Basicaly that's what i'm trying to do
};
Modelisation::~Modelisation ()
{
delete camera;
};
Planete.h
class Planete
{
private:
std::string nom;
double masse;
double diametre;
struct position { double x, y, z;};
double x,y,z;
public:
Planete (std::string nom, double masse, double diametre, double x, double y, double z);
Planete (const Planete &);
~Planete ();
};
Thanks for your help, it will be very appreciated!
You can pass it in by constant reference in the constructor then have a private members that gets initialized like so:
Modelisation(const Planete& planRef) : m_planete(planRef)Also nifty french variable names 😉
Edit:
There are some interesting design choices here you may want to reconsider global variables in favor of class members, favor smart pointers instead of dynamic allocation and deallocation the traditional way, and consider the relationships your objects have. Your simulation is a prime candidate for composition, your planets could use inheritance and polymorphism as you progress to make drawing them with the model easier later.
Edit: I was bored …