473,794 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheriting from template of self

Hello,

I have implemented a data managing template class, that takes care of
allocation of objects of type T. It's a simple array of T* that creates
new objects and returns its index so that the objects are handled
through that index.

Now for the fun part. I'm trying to create a template class for the
Managed objects, so that each object knows how to find itself (its index
and the pointer to the manager).

Here's the basic code:

//////////////////////
template <class T>
class Manager
{
public:
int CreateObject();
void DestroyObject( int idx );
(...)
private:
std::vector<T*> m_apObjects;
};

//////////////////////

template <class T>
class ManagedObject
{
public:
(...)
private:
typedef Manager<T> MyManagerType;

MyManagerType* m_pMgr;
int m_nIndex;
};

//////////////////////

My problem is that this whole concept implies creating "managed object"
classes that derive from a template of itself :
class MyManagedObj : public ManagedObject<M yManagedObj> {...};

This is not exactly the kind of code I like to write, as it looks pretty
tricky. Also, I have no idea if that is even legal c++ (vc6 anc vc7
accept it though).

Any suggestion / advices?
Thanks,

Sylvain.
Jul 23 '05 #1
1 1684

"Sylvain Audi" wrote:
My problem is that this whole concept implies creating "managed object"
classes that derive from a template of itself :
class MyManagedObj : public ManagedObject<M yManagedObj> {...};

This is not exactly the kind of code I like to write, as it looks pretty
tricky. Also, I have no idea if that is even legal c++ (vc6 anc vc7 accept
it though).

Yes, it is legal and useful. It is called Curiously Recurring Template
Pattern (CRTP).

It could be used to simulate polymorphism w/o overhead
of virtual functions (under certain circumstances).

/Pavel
Jul 23 '05 #2

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

Similar topics

4
1603
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS> <SPECIAL1>abc</SPECIAL1> <SPECIAL2>abc</SPECIAL2> ... <SPECIAL12>abc</SPECIAL9>
1
1594
by: Peter J. Bismuti | last post by:
How do you access attributes of a class when inheriting from it? Can't you just say: self.attribute? Help?! ........................................................................................................ #!/usr/bin/python from Dialog import Dialog
0
922
by: ajay.padala | last post by:
Hi I would like to be able to get the inputs that are needed into a template file programmatically. For example if we had a template: ===================== $name has $num marbles in his hand. =====================
12
6503
by: jarradw | last post by:
I am trying to figure out if this will work. I have a template base class that I want to be able to inherit from, the classes that will need to inherit from this also need to be tamplates. Is this possible or do i need to think of another way to do it?
24
2962
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
2
1703
by: snorble | last post by:
I started creating a simple "bits" class, intended to act like a array of bits. This was my initial idea, basically just overriding the string representation to display the bitmask (so far): class bits(long): def __str__ (self): s = '' if self == 0L: s += '-' else:
7
3229
by: Alex Gusarov | last post by:
Hello, I have strong .NET background with C# and want to do some familiar things from it with Python, but don't know how. For example, I created form in qt designer with QCalendarWidget, translated it into Python module and want to overload virtual method paintCell of QCalendarWidget. In C# I can write following (abstract) code: this.calendar.PaintCell += new PaintEventHandler(myPaintCellHandler); void myPaintCellHandler(object sender,...
1
3458
by: Joe Strout | last post by:
Wow, this was harder than I thought (at least for a rusty Pythoneer like myself). Here's my stab at an implementation. Remember, the goal is to add a "match" method to Template which works like Template.substitute, but in reverse: given a string, if that string matches the template, then it should return a dictionary mapping each template field to the corresponding value in the given string. Oh, and as one extra feature, I want to...
4
5556
by: l.s.rockfan | last post by:
Hello, how do i have to call an inherited, templated class constructor from the initializer list of the inheriting, non-templated class constructor? example code: template<typename T> class A
0
9518
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,...
1
10161
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,...
0
10000
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
9035
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
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
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
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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.