CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #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?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3

    Re: Avoiding virtual method in template subclass?

    Thank you, I think that will help.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Avoiding virtual method in template subclass?

    Quote Originally Posted by originalfamousjohnsmith View Post
    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?

  5. #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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Avoiding virtual method in template subclass?

    Quote 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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Avoiding virtual method in template subclass?

    Quote Originally Posted by originalfamousjohnsmith View Post
    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.

  8. #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);
    		}

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: Avoiding virtual method in template subclass?

    Quote Originally Posted by originalfamousjohnsmith View Post
    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.

  10. #10
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Avoiding virtual method in template subclass?

    How CRTP help in this case ?
    Thanks for your help.

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Avoiding virtual method in template subclass?

    Quote Originally Posted by Peter_APIIT View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured