Solved

Using Dynamic_Casts In A Templatized Subclass

Posted on 2004-09-28
11
937 Views
Last Modified: 2007-11-27
All,

I am trying to convert a function argument that is declared as a void*to a type declared at runtime for a templatized subclass.  I am getting the following compile errors.  My base class is a standard base class that a templatized subclass derives from.

In my getValue operation which is defined in MyTemplateDerivedClass, I am getting error C2680 from the following:

void getValue( void* value )
{
    T* temp = dynamic_cast<T*>(value);  <--------- error C2680

    if(temp != 0)
        *temp = mValue;  <--------------- mValue is a member variable of type T.
}

The following are the compile errors:

       C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<char>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'double *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<double>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'float *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<float>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'short *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<short>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'long *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<long>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'unsigned char *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<unsigned char>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'unsigned long *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<unsigned long>::getValue(void *)'
MyTemplateDerivedClass.h(198) : error C2680: 'unsigned short *' : invalid target type for dynamic_cast
        C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xstring(583) : while compiling class-template member function 'void __thiscall MyTemplateDerivedClass<unsigned short>::getValue(void *)'
NMAKE : fatal error U1077: 'cl' : return code '0x2'
Stop.

0
Comment
Question by:cindyclay
  • 4
  • 3
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12173114
Why are you using a 'void*' when you are already using templates?

void getValue( T* value )
{

   if(value != 0)
       *value = mValue;
}

would make a lot more sense. BTW, funny enough that C-style casts and static_cast() work for the above. Long wound term, short story: 'void' has no type, so type safe casting doesn't really make sense here...
0
 

Author Comment

by:cindyclay
ID: 12173361
The calling function doesn't care what type it's getting back because it will just pass along the value it gets back to some other entity that cares about the type.  This is the reason why the argument is a void* and the reason for the conversion.  I'll see if this works if I declare the parameter being passed in from the calling function as a void*.  Thanks for the input.  
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12173552
What you alway can do is

void* getValue( )
{
      return  dynamic_cast<void*>(mValue);
}
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12179521
You need to use static casts.

i.e.
--------8<--------
#include <iostream>
#include <string>

struct base {
      void *data;
};

template<typename T> struct derived : public base {
      derived(T t) {data = new T(t);}
      void getValue(void *value);
};

template<typename T> void derived<T>::getValue(void* value)
{
      T* temp = static_cast<T*>(value);

      if(temp != 0)
            *temp = *static_cast<T*>(data);
}

int main()
{
      derived<int> d(123);
      int value;
      d.getValue(&value);
      std::cout << "Value is " << value << '\n';

      derived<std::string> d2("Hello");
      std::string value2;
      d2.getValue(&value2);
      std::cout << "Value is " << value2 << '\n';
}
--------8<--------

But I suspect that you really ought not to be doing this anyhow. Why are you doing it?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Expert Comment

by:jkr
ID: 12179579
>> You need to use static casts.

I thought that was clear when I wrote "...BTW, funny enough that C-style casts and static_cast() work for the above..."
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12179684
Incomplete scan of your post. My apologies, jkr. No poach intended.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12181665
>> Incomplete scan of your post

Happens to me too often :o)

Anyway, I would not mix void* and templates is possible in general.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12662448
Agreed
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now