Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Dynamic_Casts In A Templatized Subclass

Posted on 2004-09-28
11
Medium Priority
?
964 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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