By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,834 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,834 IT Pros & Developers. It's quick & easy.

Solution: linking classes using their derived pointer types (type-safe!)

P: n/a
I am writing this in a new thread to alert that I found a solution to
the problem mentioned here:
and to avoid this topic getting lost before people interested in the
problem notice it.

The important tricks to the solution are two:
1) make the custom classes take a TEMPLATE argument which defines their
BASE class
2) EMBED the custom classes in a "Traits" class which is passed as
template argument to the Mesh class instead of passing them directly

(1) makes your class being a derived class that takes a generic base
class which doesn't have to be explicitly given. (2) allows you to pass
the custom classes as template arguments to the Mesh class without
immediately specifying their own template argument (i.e. their base

The final custom classes are fully defined and instantiated only inside
the Mesh class which in turn contains nested classes that become base
classes of custom classes. Just before the definition of these nested
(i.e. base) classes, there is a forward declaration of final classes
which gives template arguments to custom classes and again before it a
forward declaration of base classes.

Here is a partial implementation of the solution, showing the type-safe
inter-linking of custom Vertex and Edge classes


template <class MeshTraits>
class Mesh {

//forward declaration of base classes
class BaseVertex;
class BaseEdge;

//forward declaration of final classes (using base classes as
template arguments)
typedef typename MeshTraits :: template Vertex<BaseVertexVertex;
typedef typename MeshTraits :: template Edge<BaseEdgeEdge;

//nested base classes follow

class BaseVertex {
Edge *edge;

class BaseEdge {
Vertex *vert;


#include <stdio.h>

class MyMeshTraits {

//Vertex class taking a generic base class
template<class Baseclass Vertex : public Base {

//custom member
int a;

//function using BaseVertex edge pointer to
//get to edge's custom member

void printEdgeB() {
printf("edge's b = %d\n", Base::edge->b);

//Edge class taking a generic base class
template<class Baseclass Edge : public Base {

//custom member
int b;

//function using BaseEdge vert pointer to
//get to vert's custom member

void printVertA() {
printf("vert's a = %d\n", Base::vert->a);

int main(int argc, char **argv) {

//a little code to test things
Mesh<MyMeshTraits>::Vertex vert;
Mesh<MyMeshTraits>::Edge edge;
vert.a = 20;
edge.b = 40;
vert.edge = &edge;
edge.vert = &vert;

Sep 6 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.