Solved

Using Dynamic_Casts In A Templatized Subclass

Posted on 2004-09-28
11
960 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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