Solved

Using Dynamic_Casts In A Templatized Subclass

Posted on 2004-09-28
11
949 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

837 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