2011-04-15 81 views
0

我遇到了一个我无法解决的C++程序中的问题。 我想传递一个对象作为另一个的构造参数,但我无法弄清楚如何做到这一点。对象成员的问题

我的主要工作是创建一个模拟对象。在模拟中,我创建了两个对象平面和模型。事情是,我想通过模型化对象中的平面对象。

我的第一个对象是模拟。

#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(); 
}; 

感谢您的帮助,将不胜感激!

+0

这并不完全清楚你在这里问什么。究竟是哪条线给你带来麻烦? – 2011-04-15 15:07:44

+0

您可以尝试发布最简单的代码示例,该示例不会编译/工作,可以证明您的问题。我并不是说没有人会阅读你的代码,但这会让它变得更加困难。 – briantyler 2011-04-15 15:08:24

+0

你说得对,那就是我要做的。 – Athanase 2011-04-15 15:09:51

回答

2

您可以通过在构造函数中不断引用传递则有被初始化像这样一个私有成员:

Modelisation(const Planete& planRef) : m_planete(planRef)

而且漂亮的法国变量名;)

编辑:

在这里有一些有趣的设计选择,你可能想重新考虑全局变量而赞成类成员,支持智能指针而不是动态分配和取消分配传统方式,并考虑您的对象具有的关系。您的模拟是组合的主要候选对象,您的行星可以在继续使用继承和多态性以便稍后使用模型绘制模型时使用。

编辑:我很无聊......

class Planete 
{ 
public: 
    Planete (const std::string& nom, double masse, double diametre, double x, double y, double z) 
     : m_nom(nom), m_masse(masse), m_position(x, y, z) {} 
    virtual ~Planete() {} 

    // default implementation as most planets don't have people 
    virtual unsigned long HumanPopulation() { return 0; } // virtual with default implemenation, each planet can change it 
    virtual double Temp() = 0; // pure virtual each planet must have it's own version 
protected: 
    std::string m_nom; 
    double m_masse; 
    double m_diametre; 
    class Position 
    { 
    public: 
     Position(double x, double y, double z) 
      : x_(x), y_(y), z_(z) {} 
    private: 
     double x_, y_, z_; 
    }; 
    Position m_position; 
}; 

class Terre : public Planete // Earth is a Planet 
{ 
public: 
    Terre(double masse, double diametre, double x, double y, double z) 
     : Planete("Terre", masse, diametre, x, y, z) {} 
    ~Terre() {} 

    // override base class default virtual 
    unsigned long HumanPopulation() { return CalculatePeople(); } 
    double Temp() { /* yata yata */ } 

private: 
    unsigned long CalculatePeople() { /* figure out how many people on earth */ } 
}; 

class Mercure : public Planete // Mecury is also a Planet 
{ 
public: 
    Mercure(double masse, double diametre, double x, double y, double z) 
     : Planete("Mercure", masse, diametre, x, y, z) {} 
    ~Mercure() {} 

    double Temp() { /* tres chaud */ } 
}; 
1

你的意思是这样的吗?

Modelisation::Modelisation (int argc, char **argv, char nomFenetre [] , 
int hauteur, int largeur, int x, int y, Planet * planet) 
{ 

}; 


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, Obj); 
}; 
1

我不知道你问什么,但这个工作得很好:

Simulation::Simulation (Planete* planete, Model* model) 
{ 
    Obj = planete; 
    Model = model; 
}; 

其他一些注意事项:

  • ObjModel确实应该在Simulation成员变量,而不是全局变量在你r Simulation.cpp
  • 我认为它是一个主要的代码气味将argc, argv作为参数传递给构造函数。相反,您的main方法应解析命令行参数,然后将解析的值传递给构造函数。
0

您需要在这里更改几件事情。首先,Planete和Modelisation变量应该是模拟的成员,而不是全局变量。所以,你的模拟声明是这样的:

class Simulation 
{ 
public: 
    Simulation(int argc, char **argv); 
    virtual ~Simulation(); 

private: 
    Planete *Obj; 
    Modelisation *Model; 
} 

关于通过你PLANETE对象的Modelisation,你也可以有Modelisation有PLANETE成员如下:

class Modelisation 
{   
    // stuff you already have... 
private: 
    Planete *planete; 
} 

然后你就可以改变你的Modelisation构造采取PLANETE指针,并在构造函数中分配给它:

Modelisation (Planete *p, int argc, char **argv, char[], int, int, int, int) 
{ 
    // Stuff that's already there... 
    planete = p;  
} 

现在你有,因为你必须确保即将这里要小心谁拥有Planete,谁应该删除它。删除它两次会导致不好的事情。我建议使用shared_ptr避免在那里做错事。