Solved

Using Dynamic_Casts In A Templatized Subclass

Posted on 2004-09-28
11
944 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 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