-
November 22nd, 2009, 04:46 AM
#1
Avoiding virtual method in template subclass?
I want to make a template that subclasses a parent but changes just one method.
This should be easy, technically speaking, since it should all be generated code. Virtual method is not necessary, but how do I tell the compiler to see the new method?
So for example:
Code:
template <typename type> class CalcA
{
protected:
long calculate();
};
template <typename type> class CalcB: public CalcA<type>
{
protected:
long calculate();
};
It's forcing me to use virtual methods, but there has to be some way around this. Anyone know?
-
November 22nd, 2009, 09:38 AM
#2
Re: Avoiding virtual method in template subclass?
You can use the Curiously Recurring Template Pattern to call a member function of the derived class without using virtual functions.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 22nd, 2009, 04:46 PM
#3
Re: Avoiding virtual method in template subclass?
Thank you, I think that will help.
-
November 23rd, 2009, 09:46 AM
#4
Re: Avoiding virtual method in template subclass?
Originally Posted by originalfamousjohnsmith
It's forcing me to use virtual methods, but there has to be some way around this. Anyone know?
Why do you want to avoid virtual functions?
-
November 24th, 2009, 02:31 AM
#5
Re: Avoiding virtual method in template subclass?
Because of performance and about 2 million other reasons. I don't mind virtual methods but it would be a big mistake here.
Not sure there are any better options for me, though. The suggested solution is a possibility but it's a little complex. Like I said it's two one line methods I want to change, but if I did it that way I'd need to change every place I call those two methods and doing that everywhere will make an unreadable mess.
I may as well spell out the whole problem. I have a vector class, and I want to have it so that it can either use the default allocation or it can use a different allocation method for when it's being used as a system object. However, I don't want things to get too complex, and I don't want to add in extra performance penalties, and I especially don't want its usage to change (ie extra unwieldly syntax every time you create something).
I guess the options are:
1. Preprocessor. Kind of ugly, but I can generate out a duplicate with small changes using preprocessor.
2. File generation. I hate doing this, but it does work.
3. Allocator helper object. Then I have to pass one in somehow, and I just transfer the problem of virtualness. Virtual inline static methods are almost guaranteed to make the compiler go crazy. Or else I have a few static concrete allocator objects, which I have to ensure get initialized before anything else.
4. function pointers. No inlining for certain, then, but at least will work.
5. CRT - kind of like 3, I guess.
6. boolean? I can't use a switch it boils down to same problem with needing to be virtual, but I can stick a boolean in, though it increases class size.
7. something else?
Last edited by originalfamousjohnsmith; November 24th, 2009 at 02:44 AM.
-
November 24th, 2009, 02:58 AM
#6
Re: Avoiding virtual method in template subclass?
Originally Posted by originalfamousjohnsmith
I have a vector class, and I want to have it so that it can either use the default allocation or it can use a different allocation method for when it's being used as a system object.
Must this decision be made at run time, or can it always be made at compile time?
-
November 24th, 2009, 04:06 AM
#7
Re: Avoiding virtual method in template subclass?
Originally Posted by originalfamousjohnsmith
7. something else?
Can't you use templates to make a policy out of the allocation strategy, like
Code:
template <typename AllocPolicy>
class CalcA {
public:
void calculate() {
AllocPolicy::calculate();
}
};
Last edited by nuzzle; November 24th, 2009 at 04:13 AM.
-
November 24th, 2009, 05:03 AM
#8
Re: Avoiding virtual method in template subclass?
Ok, yes I guess it's basically a 'policy'. Some of this does not gel well in my brain, but I see how helpful a policy can be now and what it means, and it is sort of like the CRT.
Anyway I end up with code like:
Code:
template <typename type, class AllocationClass=SStandardAllocation> class Array: public SBaseArray, public AllocationClass
{
protected:
type *contents;
long count;
long contiguousSize;
long totalSize;
SSimpleChunk* firstChunk;
inline void* allocate(size_t size)
{
return AllocationClass::allocate(size);
}
inline void deallocate(void* address, size_t size)
{
AllocationClass::deallocate(address, size);
}
-
November 24th, 2009, 06:04 PM
#9
Re: Avoiding virtual method in template subclass?
Originally Posted by originalfamousjohnsmith
Some of this does not gel well in my brain,
Well, if you want polymorphism there's either object orientation or generics. If you rule out virtual methods there's only templates left.
-
November 26th, 2009, 01:53 AM
#10
Re: Avoiding virtual method in template subclass?
How CRTP help in this case ?
Thanks for your help.
-
November 26th, 2009, 05:30 AM
#11
Re: Avoiding virtual method in template subclass?
Originally Posted by Peter_APIIT
How CRTP help in this case ?
Read the link I provided. It explains how you can simulate virtual functions without the runtime overhead using CRTP.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|