Well,I've derived aclass from an abstract one and I want to define a friend function( operator<< ) that would be polymorphic.
Here are the specs of the classes:
#ifndef SHAPE_HPP
#define SHAPE_HPP
#include <iostream>
#include <fstream>
using std::ostream;
using std::ofstream;
class Shape
{
public:
Shape();
~Shape();
virtual void draw()const=0;
};
Shape::Shape()
{
}
Shape::~Shape()
{
}
#endif
And the derived class:
#ifndef RECTANGLE_HPP
#define RECTANGLE_HPP
#include <iostream>
#include <fstream>
#include <vector>
#include <GL/GL.h>
#include <GL/glu.h>
#include "Vector3D.hpp"
#include "Shape.hpp"
using std::vector;
using std::ostream;
using std::ofstream;
using std::cout;
using std::endl;
template<class T>
class Rectangle : public Shape
{
public:
Rectangle();
Rectangle( const T& , const T& );
~Rectangle();
void draw()const;
template<class U> friend ostream& operator<<( ostream& , const Rectangle<U>& );
template<class U> friend ofstream& operator<<( ofstream& , const Rectangle<U>& );
protected:
T a;
T b;
vector<Vector3<T> > vertices;
};
template<class T>
Rectangle<T>::Rectangle()
{
}
template<class T>
Rectangle<T>::Rectangle( const T& _a , const T& _b )
{
a = _a;
b = _b;
vertices.reserve( 4 );
vertices.push_back( Vector3<T>( -a/2 , -b/2 , 0 ) );
vertices.push_back( Vector3<T>( a/2 , -b/2 , 0 ) );
vertices.push_back( Vector3<T>( a/2 , b/2 , 0 ) );
vertices.push_back( Vector3<T>( -a/2 , b/2 , 0 ) );
}
template<class T>
Rectangle<T>::~Rectangle()
{
}
template<class T>
void Rectangle<T>::draw()const
{
}
template<class T>
ostream& operator<<( ostream& out , const Rectangle<T>& s )
{
for( int i = 0 ; i < 4 ; ++i )
{
out << i << ":(" << s.vertices[0] << "," << s.vertices[1] << "," << s.vertices[2] << ")" << endl;
}
return out;
}
template<class T>
ofstream& operator<<( ofstream& out , const Rectangle<T>& s )
{
for( int i = 0 ; i < 4 ; ++i )
{
out << i << ":(" << s.vertices[0] << "," << s.vertices[1] << "," << s.vertices[2] << ")" << endl;
}
return out;
}
template<>
void Rectangle<float>::draw()const
{
glBegin( GL_QUADS );
glVertex3f( vertices[0][0] , vertices[0][1] , vertices[0][2] );
glVertex3f( vertices[1][0] , vertices[1][1] , vertices[1][2] );
glVertex3f( vertices[2][0] , vertices[2][1] , vertices[2][2] );
glVertex3f( vertices[3][0] , vertices[3][1] , vertices[3][2] );
glEnd();
}
template<>
void Rectangle<double>::draw()const
{
glBegin( GL_QUADS );
glVertex3d( vertices[0][0] , vertices[0][1] , vertices[0][2] );
glVertex3d( vertices[1][0] , vertices[1][1] , vertices[1][2] );
glVertex3d( vertices[2][0] , vertices[2][1] , vertices[2][2] );
glVertex3d( vertices[3][0] , vertices[3][1] , vertices[3][2] );
glEnd();
}
#endif
I want actually this:
Shape* s = new Rectangle<double>( 50.0 , 20.0 );
cout << s << endl;
How can I achieve this?
[Edited by - johnstanp on May 6, 2008 6:29:14 AM]