![]() |
|
#1
|
|||
|
|||
Do I need RTTI?I have a hierarchy of C++ classes that need to each have a different-sized array declared in the parent but allocated in the children. I also need the array size to be available through the reference (without a cast), so there has to be a non-static copy.
CPP / C++ / C Code:
Last edited by LuciWiz : 24-Aug-2006 at 16:29.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Do I need RTTI?BTW, I'm trying to skip over the whole mess of dynamic allocation here if I can do without.
I think some virtual functions might help with the "no casting" thing, but I don't want to redefine them for each child, and that still only solves half of the problem. Also the array type could change to a subtype in some children. |
|
#3
|
|||
|
|||
Re: Do I need RTTI?I found a pretty good solution. There are two pure virtual methods, an accessor for the size and a lookup for the item, in the base. The children have a static const of the size because they need it in two places (the accessor and the array declaration), and they define methods. That way the interface to the array is through a virtual method and the implementation can be child-specific; I statically allocated the array from the child class.
Hope that helps if anyone was actually following this. |
|
#4
|
||||
|
||||
Re: Do I need RTTI?Quote:
That's what I wanted to suggest It would have been pretty easy this way, since you could just make the size of the array a protected member variable in the parent class, then assign it to a different value in the constructor of each child class, and dynamically allocate the array to that size. Well, I see you found another solution (more complicated IMO). Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
|||
|
|||
Re: Do I need RTTI?Quote:
WHY!? This practically SCREAMS for dynamic allocation. Why go through all of the complexities associated with trying to get around it? In fact, in C++, the allocation is TRIVAL if you use a standard container or write an appropriate template class or even a template member function in this case. The beauty of C++ is that we have a ctor and dtor and we can handle initialization and deallocation in well-known, appropriate places whereas in C, we are left to our own devices and we each may pick a separate way to do it. :davis: |
|
#6
|
|||
|
|||
Re: Do I need RTTI?Wow, I seem to have stepped on some toes...
The reason I'm trying to avoid dynamic allocation here is that this entire project is going to be a mess of dynamic allocation (I know because it's a reincarnation of an old project that died because it was a mess of dynamic allocation filled with cyclical dependencies), and I'm clinging to my sanity while I can. In the proposed dynamic solution, you'd need to make the size not a constant but a variable and then set it to a constant at runtime. That sounds frivolous and makes me uneasy. Combine that with the fact that external accesses to this dynamically allocated array will know the size only through that variable, and you've got a potential (granted, a really small one) for problems. I'm already using lots of virtual functions, anyway, so it won't be a performance bottleneck or anything. It'll all get dynamically allocated along with the object, anyway... I'm not afraid of dynamic allocation, but I tried it both ways and I think this way works better. Well, I've been typing long enough that Mr. Banana Guy Thing stopped dancing at me, so I'll leave it at that for now. |
|
#7
|
|||||
|
|||||
Re: Do I need RTTI?Quote:
No, just trying to understand why you're avoiding dynamic allocation. Quote:
Why not use a simple container? CPP / C++ / C Code:
...and you're done with "dynamic allocation" as the container will manage it for you and when it goes out of scope, it will be deallocated for you. Quote:
Huh? Quote:
What if you have some variable for the "dynamic" size and use something like: CPP / C++ / C Code:
...it seems rather simple to me. You can easily add the allocation to your ctor and the deallocation in your dtor. Quote:
I typed this response and a bunch of code, ran some regression tests and memory leak checks (valgrind) and the banana guy is still jumping around like a fruit cake on speed. I wonder what that means? :davis: |
|
#8
|
|||
|
|||
Re: Do I need RTTI?Quote:
Quote:
Anyway, I'll keep the dynamic allocation in mind, but for right now, it works and I've moved on to other sections. Quote:
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The