473,834 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Meta class programming question

I have a class that is basicaly duplicated throughout several files with
only members names changing according to the class name yet with virtually
the exact same coding going on.


class A
std::vector<B*> Bs;
A() { blah; }

void SomeFunc_A() {blah blah blah};

class A
std::vector<C*> Cs;
B() { blah; }

void SomeFunc_B() {blah blah blah};

and eventually one of the classes is a "terminal" in the sense it does not
continue the same abstract recursive process. My question is, is there any
way to automate this process? It seems I cannot use templates because I
cannot generate the proper method names are derived from the name of the

i.e., I can't do something like

Metaclass template <class name, class subname>
class name
std::vector<sub name *> subname's';
name() {blah}

void SomeFunc_@'name ' () { blah blah blah}

where say the 's' is appended to the subname literal and the @ appends the
string to the someFunc literal...

then I could do something like

Metaclass<A, B> and it produces the class Agiving above and Metaclass<B,C>
would produce B.
The reason I do this is because right now I have 7 files in my project that
all are virtually exactly the same as just 1(and how I got them was basicaly
using find and replace 3 times to turn the class into a "new" one that is
virtually identical with the other(excep ofcourse the coding is actualy the
same with different literals)).

My question is, is there any way in C++ to do something like this(I know my
code is not valid C++ code above but just giving some example). I just need
to generate a class from a meta class that also can change the method's
names that will be based on the name of the class.

Actually I will give a more concrete example that I actually kinda use in my

class Score : public Base
std::vector<Sta ff> Staffs;
int NumStaffs() { return (int)Staffs.siz e(); }

class Staff : public Base
std::vector<Bar > Bars;
int NumBars() { return (int)Bars.size( ); }

class Bar : public Base

class Base
std::vector<Pro perty> Properties;
int NumProperties() { return (int)Properties .size(); }

class Property

and I would want to access methods like:

Score.NumStaffs ();

I know I could make some general template where all methods across the
different classes would have the same name so I might have

Score.NumObject s(); which would return the number of objects in in the
vector in that object and(Staffs in this cass)
Score.Objects[0].NumObjects(); would return same as above but for the Bars.

The problem with this is I only need the recursiveness for about 5 classes
and that I want the naming to follow the modelling I'm using(modeling of
Score->Staff->Bar->Notes of music) else it might be confusing to remeber

Score.Objects[0].Objects[3].NumObjects(); is refering to

So thats the idea... as you can see most the classes are exactly the same
except for the naming of functions. I do see a problem now with my example
above about just adding 's' to the end... I wouldn't want Propertys but
Properties so there would have to be a way of creating logical english
literal names(i.e., replace ending y with ies when adding s), etc...

Anything that can do something like this out there? (would save me a lot of
work of having to to mess with 7 files that are identical except for naming
and potential other problems)


Sep 7 '05
21 4090
Jon Slaughter wrote:
"Tim Clacy" <no*******@nosp amphaseone.nosp amdk> wrote in message
news:43******** *************** @dread12.news.t ele.dk...
Ben Pope wrote:
Jon Slaughter wrote:
"Ben Pope" <benpope81@_pan ts_gmail.com> wrote in message
news:1126171026 .d07cac1e0dd2a9 5b08c5ce3e65cb3 fcf@teranews...

> You will need to forward declare the class and use pointers, you
> can't
> have a class as a direct member of itself, because the size cannot
> be determined (it's recursive).
Yes, but if I could someone create a template of the first one that
is not recursive and use that one in the recursive one then it
wouldn't really be recursive any more(well at most 1 lvl deep).
Thats what I was trying to do before but it didn't work out. I
mean, basicaly the size can be determined if its recusive but th
the recursion has some finite end.. I think the problem, atleast it
seems, is that C++ doesn't have a way to know when to stop the
recursion. if I could do something like

class X
X@3 Properties;

where the compiler would interpret @3 only accept 3 lvls deep and
when it gets to the 4 it removes that declaration then the
recursion would end. I suppose the problem with something like
that is it
just creates more problems ;)

Ahh yes, but the compiler is unlikely to be able to do that.

I'm not sure where it would get you, but you could probably do
something like:

template<int I>
class MyClass{
MyClass<I-1>* child_;

And start off with:

MyClass<4> root;

But I'm not sure how you would terminate when I-1 = 0.

You would specialise the template for the case 'I equals 0'; this
will terminate the recursion.
class MyClass<0>{

The MyClass<4> type would then look like this:

MyClass<4> {
MyClass<3>* child_;

MyClass<3> {
MyClass<2>* child_;

MyClass<2> {
MyClass<1>* child_;

MyClass<1> {
MyClass<0>* child_; // No more kids after this one

AWESOME!!! Almost exactly what I need! it does the job almost
perfectly but one complaint is that the terminal class is still
represented... but since its empty its not that big a deal.

Is there anywhere I can learn more about "advanced" template
programming? This recursive ability seems like a very powerful
feature and I never expected C++ to be able to do it. Surely there
are more powerful features like this?

There's quite a bit of material on the Internet (google C++ template
I'm virtually completely new to the concept of templates and so I
just have a basic grasp of what they do but I can't seem to find any
good information online about them. Documentation isn't what it used
to be and it seems like I just end up going around in circles trying
to find something specific but never get it.

Is there any book worth that goes into a good deal of depth on the
"advanced" features of C++. (I'm not saying that the above is
advanced but that it not something that I would have ordinarily
thought possible because, mainly, I didn't know you could have a
"template counter" and specialize it).

Thanks for the help!! I really appreciate it!

Still might run into some problems but I think I can probably hack it


Sep 12 '05 #21
Tim Clacy wrote:
Ben Pope wrote:
But I'm not sure how you would terminate when I-1 = 0.

You would specialise the template for the case 'I equals 0'; this will
terminate the recursion.
class MyClass<0>{

Good work.

I'm not just a number. To many, I'm known as a String...
Sep 12 '05 #22

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

Similar topics

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...
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,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.