N
Naomi
I need to make software engineering decision to do with using a
derived data type in a container class. So for example, if I have an
Edge class, and I want to make a Edge object which contains two
NumberVertices instead of Vertices, I can either just use a non-
templated Edge class which stores pointers to my NumberVertices and
leave it up to the programmer to cast back again to NumberVertex (this
type of casting only works with pointers), or I can use a template
class.
I've been looking around on info on this, and I can't decide which is
a better approach. I find using template classes gives you very
difficult to interpret error messages, but I wonder if using this
pointer/static cast approach will give me some typing problems later
on.
/***************************************************
* Using pointers and static casts to to access objects of derived
class:
****************************************************/
#include <iostream>
class Vertex
{
public:
Vertex(void){};
};
class NumberVertex :
public Vertex
{
public:
NumberVertex(void){};
NumberVertex(int num):number(num){};
int getNum(){return number;}
public:
~NumberVertex(void){};
protected:
int number;
};
class Edge
{
public:
Edge(Vertex* a, Vertex* b):v1(a),v2(b){};
Vertex* getV1(){return v1;}
Vertex* getV2(){return v2;}
protected:
Vertex* v1;
Vertex* v2;
};
int main(int argc, char **argv){
NumberVertex* v1 = new NumberVertex(1);
NumberVertex* v2 = new NumberVertex(2);
Edge e(v1, v2);
NumberVertex* ev1 = (NumberVertex*) e.getV1();
NumberVertex* ev2 = (NumberVertex*) e.getV2();
std::cout << "V1: " << ev1->getNum() << std::endl;
std::cout << "V2: " << ev2->getNum() << std::endl;
}
/***************************************************
* Using template class:
****************************************************/
#include <iostream>
class Vertex
{
public:
Vertex(void){};
};
class NumberVertex :
public Vertex
{
public:
NumberVertex(void){};
NumberVertex(int num):number(num){};
int getNum(){return number;}
public:
~NumberVertex(void){};
protected:
int number;
};
template <class Vert> class Edge
{
public:
Edge(Vert& a, Vert& b):v1(a),v2(b){};
Vert& getV1(){return v1;}
Vert& getV2(){return v2;}
protected:
Vert v1;
Vert v2;
};
derived data type in a container class. So for example, if I have an
Edge class, and I want to make a Edge object which contains two
NumberVertices instead of Vertices, I can either just use a non-
templated Edge class which stores pointers to my NumberVertices and
leave it up to the programmer to cast back again to NumberVertex (this
type of casting only works with pointers), or I can use a template
class.
I've been looking around on info on this, and I can't decide which is
a better approach. I find using template classes gives you very
difficult to interpret error messages, but I wonder if using this
pointer/static cast approach will give me some typing problems later
on.
/***************************************************
* Using pointers and static casts to to access objects of derived
class:
****************************************************/
#include <iostream>
class Vertex
{
public:
Vertex(void){};
};
class NumberVertex :
public Vertex
{
public:
NumberVertex(void){};
NumberVertex(int num):number(num){};
int getNum(){return number;}
public:
~NumberVertex(void){};
protected:
int number;
};
class Edge
{
public:
Edge(Vertex* a, Vertex* b):v1(a),v2(b){};
Vertex* getV1(){return v1;}
Vertex* getV2(){return v2;}
protected:
Vertex* v1;
Vertex* v2;
};
int main(int argc, char **argv){
NumberVertex* v1 = new NumberVertex(1);
NumberVertex* v2 = new NumberVertex(2);
Edge e(v1, v2);
NumberVertex* ev1 = (NumberVertex*) e.getV1();
NumberVertex* ev2 = (NumberVertex*) e.getV2();
std::cout << "V1: " << ev1->getNum() << std::endl;
std::cout << "V2: " << ev2->getNum() << std::endl;
}
/***************************************************
* Using template class:
****************************************************/
#include <iostream>
class Vertex
{
public:
Vertex(void){};
};
class NumberVertex :
public Vertex
{
public:
NumberVertex(void){};
NumberVertex(int num):number(num){};
int getNum(){return number;}
public:
~NumberVertex(void){};
protected:
int number;
};
template <class Vert> class Edge
{
public:
Edge(Vert& a, Vert& b):v1(a),v2(b){};
Vert& getV1(){return v1;}
Vert& getV2(){return v2;}
protected:
Vert v1;
Vert v2;
};