472,792 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,792 software developers and data experts.

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

I am writing this in a new thread to alert that I found a solution to
the problem mentioned here:
http://groups.google.com/group/comp....70afaa089fd5b8
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
class).

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

//Mesh.h
////////////////////////////////////////////////////////

template <class MeshTraits>
class Mesh {
public:

//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 {
public:
Edge *edge;
};

class BaseEdge {
public:
Vertex *vert;
};
};

//MeshTest.cpp
////////////////////////////////////////////////////////

#include <stdio.h>

class MyMeshTraits {
public:

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

//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 {
public:

//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;
vert.printEdgeB();
edge.printVertA();
};

Sep 6 '06 #1
0 1963

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
24
by: laredotornado | last post by:
Hello, Are all pointer types the same length? My instinct tells me yes, but I just wanted to confirm with the experts. So if I have typedef struct { char* field1; int field2; }...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
1
by: rbg | last post by:
I am using derived tables to Page data on the SQL Server side. I used this link as my mentor for doing paging on the SQL Serverhttp://msdn2.microsoft.com/en-us/library/ms979197.aspx I wanted to...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
3
by: rajanipro | last post by:
Hi buddies! Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case? using System; class ParentClass { public static...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
1
by: ladesidude | last post by:
Hi, I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated. ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.