473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

casting in generic

I have needed to cast a pointer with different types for get a value.
I have try to use generic, but there are cast exception.
For example the following method:

generic<typename T> void XClass::GetValue(T gValue)
{
int iValue=34234234;
void *vValue=&iValue;

gValue=(T)*vValue;
}

can't be used for compile error (a illegal indirection method)
modifying the method in this way:

generic<typename T> void XClass::GetValue(T gValue)
{
int iValue=34234234;
void *vValue=&iValue;

unsigned long ulValue;
ulValue=*((unsigned long*)vValue);

gValue=(T)ulValue;
}

there's a runtime error (Specified cast is not valid)

How can I to use generic for this goal?

Thanks for reply

EncOdEd

Apr 22 '06 #1
2 1396
>I have needed to cast a pointer with different types for get a value.
I have try to use generic, but there are cast exception.
For example the following method:

generic<typename T> void XClass::GetValue(T gValue)
{
int iValue=34234234;
void *vValue=&iValue;

gValue=(T)*vValue;
}
you are dereferencing a void*. that will not work, since void doesn't have a
type.

can't be used for compile error (a illegal indirection method)
modifying the method in this way:

generic<typename T> void XClass::GetValue(T gValue)
{
int iValue=34234234;
void *vValue=&iValue;

unsigned long ulValue;
ulValue=*((unsigned long*)vValue);

gValue=(T)ulValue;
}

there's a runtime error (Specified cast is not valid)


What is the type that you are working with? I.e. what is T?
another thing: if T is just a value type, then changing gValue will not have
an effect in the scope of the caller.
If all you need to do is to cast one value to another, there is no need to
use a generic class unless this is part of something larger.

Could you describe what it is you are trying to do?

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 22 '06 #2
>What is the type that you are working with? I.e. what is T?
T is a value type.
another thing: if T is just a value type, then changing gValue will not have
an effect in the scope of the caller.


You are right, the method is this:

generic<typename T> void ConvertClass::GetValue(T% gValue)
{
int iValue=34234234;
void *vValue=&iValue;

unsigned long ulValue;
ulValue=*((unsigned long*)vValue);

gValue=(T)ulValue;
}

This method is only a example, the real method reads a file mapped in
memory that contains different value type. I could create different
methods: GetRealValue(), GetIntValue(), GetLongValue() etc.. I would
like to create only one Method like that before described;

Consider: in the method I can't use this syntax: ulValue=*((T*)vValue)
because of this error: "indirections on a generic type parameter are
not allowed", so I cast the value to a big value:

ulValue=*((unsigned long*)vValue);

than I cast to the type generic:

gValue=(T)ulValue;

Thanks for your help.

Alessandro

Apr 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
2
by: cylt | last post by:
Hi, I would like to have something like that : IList<IUser> list2 = list1 as IList<IUser>; where list1 is a generic collection of User ( IList list1<User>=new List<User>() ) and where User...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
3
by: Vincent Finn | last post by:
Hi, I am trying to write a generic function and it isn't behaving as expected. I want to avoid having to write custom convert functions for my enums so I want to convert to an int and cast to...
3
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.