Thread: Class question

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Wouldnt the use of dynamic_cast remove some of the point of designing my primitive class as I have done?

    I am steeling the idea of how to do this primitive/ scene class from a tutorial on how to write a raytracer, and in that tutorial they do it as I do, just without the erros.'

    Code:
    // -----------------------------------------------------------
    // Primitive class definition
    // -----------------------------------------------------------
    
    class Primitive
    {
    public:
    	enum
    	{
    		SPHERE = 1,
    		PLANE
    	};
    	Primitive() : m_Name( 0 ), m_Light( false ) {};
    	Material* GetMaterial() { return &m_Material; }
    	void SetMaterial( Material& a_Mat ) { m_Material = a_Mat; }
    	virtual int GetType() = 0;
    	virtual int Intersect( Ray& a_Ray, float& a_Dist ) = 0;
    	virtual vector3 GetNormal( vector3& a_Pos ) = 0;
    	virtual Color GetColor( vector3& ) { return m_Material.GetColor(); }
    	virtual void Light( bool a_Light ) { m_Light = a_Light; }
    	bool IsLight() { return m_Light; }
    	void SetName( char* a_Name );
    	char* GetName() { return m_Name; }
    protected:
    	Material m_Material;
    	char* m_Name;
    	bool m_Light;
    };
    
    // -----------------------------------------------------------
    // Sphere primitive class definition
    // -----------------------------------------------------------
    
    class Sphere : public Primitive
    {
    public:
    	int GetType() { return SPHERE; }
    	Sphere( vector3& a_Centre, float a_Radius ) : 
    		m_Centre( a_Centre ), m_SqRadius( a_Radius * a_Radius ), 
    		m_Radius( a_Radius ), m_RRadius( 1.0f / a_Radius ) {};
    	vector3& GetCentre() { return m_Centre; }
    	float GetSqRadius() { return m_SqRadius; }
    	int Intersect( Ray& a_Ray, float& a_Dist );
    	vector3 GetNormal( vector3& a_Pos ) { return (a_Pos - m_Centre) * m_RRadius; }
    private:
    	vector3 m_Centre;
    	float m_SqRadius, m_Radius, m_RRadius;
    };
    Code:
    m_Primitive = new Primitive*[100];
    	// ground plane
    	m_Primitive[0] = new PlanePrim( vector3( 0, 1, 0 ), 4.4f );
    	m_Primitive[0]->SetName( "plane" );
    	m_Primitive[0]->GetMaterial()->SetReflection( 0 );
    	m_Primitive[0]->GetMaterial()->SetDiffuse( 1.0f );
    	m_Primitive[0]->GetMaterial()->SetColor( Color( 0.4f, 0.3f, 0.3f ) );
    	
    	// big sphere
    	m_Primitive[1] = new Sphere( vector3( 1, -0.8f, 3 ), 2.5f );
    	m_Primitive[1]->SetName( "big sphere" );
    	m_Primitive[1]->GetMaterial()->SetReflection( 0.6f );
    	m_Primitive[1]->GetMaterial()->SetColor( Color( 0.7f, 0.7f, 0.7f ) );
    As you see, here all object can be treathed the same, which makes all much more easy.
    Also, with this code I can call primitve[0].intersection(), so that I dont have to think about what kind of object I am testing. So, what am I doint wrong as my code dont work, but this does?

    Thanks

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    m_Primitive[1] = new Sphere( vector3( 1, -0.8f, 3 ), 2.5f );
    This is the key difference: constructor arguments.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As you see, here all object can be treathed the same, which makes all much more easy.
    I note that every time you subclass Primitive in that class design, you need to alter it.

    Wouldnt the use of dynamic_cast remove some of the point of designing my primitive class as I have done?
    As CornedBee mentioned, you should consider using an appropriate constructor first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    630
    What is wrong with dynamic_cast?
    I often use boost::dynamic_pointer_cast for smart pointers.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The use of dynamic_cast suggests that your class hierarchy design has somehow failed: perhaps your base class was not expressive enough, or a function is getting data from the wrong place.
    There are situations where dynamic_cast is necessary, but all literature agrees that if you use dynamic_cast "often", there's a problem.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    630
    boost::dynamic_pointer_cast, dynamic_cast - is that the same/similar, regarding what you previously said about dynamic_cast?

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes. The difference is that dynamic_pointer_cast works with pointers and smart pointers alike. (Not sure which smart pointers beyond shared_ptr are supported.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM