473,725 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<BaseVert exVertex;
typedef typename MeshTraits :: template Edge<BaseEdgeEd ge;

//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<MyMeshTrai ts>::Vertex vert;
Mesh<MyMeshTrai ts>::Edge edge;
vert.a = 20;
edge.b = 40;
vert.edge = &edge;
edge.vert = &vert;
vert.printEdgeB ();
edge.printVertA ();
};

Sep 6 '06 #1
0 2032

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

Similar topics

11
3609
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
24
1460
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; } myStruct; myStruct *a;
5
5346
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: const char * : "pointer to const-qualified char". char *: "pointer to char". Are these pointed-to types compatibles?
6
2943
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 calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
1
4146
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 use USER PAGING, thus I used the following code: CREATE PROCEDURE UserPaging ( @currentPage int = 1, @pageSize int =1000 ) AS
8
2807
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, I have a where clause which states : where PermitID like @WorkType order by WorkStart DESC
3
1840
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 int i;
5
3911
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 static_cast<>? In other words, is there any real difference between statements like (with non-pointer types): double a = 3.4; int b = (int)a; // <--- this
1
7331
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. #include <stdio.h> #define Size1 (4) #define Size2 (3)
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.