Problems with tertiary operator and reinterpret_cast

T

Taran

Hi All,

I was trying some code which essentially does what the function 'func'
here does. To simplify the things and make a consise post I have
created a dummy app to show my problem.
The issue was to assign a value to an array of pointers. The selection
of array depends upon a condition and the void* ptr has to be casted to
the correct type and inserted in the array.


//file1.cpp

void * obj_arr;

class class_A
{
};

class class_B
{
};

class_A *a_arr[5] ;
class_B *b_arr[7];

class_A *a_obj ;
class_B *b_obj;


void func(bool is_class_A, void* void_ptr, void* new_ptr, int at_index)
{
is_class_A? \
(*(reinterpret_cast<class_A*>(void_ptr) + at_index) =
(reinterpret_cast<class_A*>(new_ptr))) : //line 1
(*(reinterpret_cast<class_B*>(void_ptr) + at_index) =
(reinterpret_cast<class_B*>(new_ptr))); // line 2
}


int main()
{
obj_arr = reinterpret_cast<void*>(a_arr);
void* new_ptr = reinterpret_cast<void*>(a_obj);
func(true, obj_arr, new_ptr,0);

obj_arr = reinterpret_cast<void*>(b_arr);
new_ptr = reinterpret_cast<void*>(b_obj);
func(false, obj_arr,new_ptr,1);
return 0;
}


Errors:
Compiling...
file1.cpp
file1.cpp(21) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_A *' (or there is no
acceptable conversion)
file1.cpp(22) : error C2679: binary '=' : no operator defined which
takes a right-hand operand of type 'class class_B *' (or there is no
acceptable conversion)

Error executing cl.exe.

sample.exe - 2 error(s), 0 warning(s)


I have thought about the error and cannot find a problem with the cast
in the 'func'.
And specially the error thrown, both side are of correct type, pointers
to the correct class, so assignment shouldn't be any problem.

What I am doing wrong here? Am I missing something?

Any help or pointers will be appreciated.

Thanks in Advance.
 
H

Heinz Ozwirk

Taran said:
void func(bool is_class_A, void* void_ptr, void* new_ptr, int at_index)
{
is_class_A? \
(*(reinterpret_cast<class_A*>(void_ptr) + at_index) =
(reinterpret_cast<class_A*>(new_ptr))) : //line 1
(*(reinterpret_cast<class_B*>(void_ptr) + at_index) =
(reinterpret_cast<class_B*>(new_ptr))); // line 2
}

There are two problems, one related to assignment and one related to the ?:
operator. Ignoring the second problem for a while you basically have
something like

int *p, *q;
*p = q;

That is, you are trying to assign a pointer to some object, to an instance
of the object's type.

The second problem as to do with the ?: operator. Its third argument must be
convertible to the type of the second one. So this will only work if there
is a valid conversion between class_A and class_B.

Finally you should avoid void pointers whereever possible. It would be much
better to have to functions, one dealing with objects and arrays of class_A
and one for objects and arrays of class_B. If you don't like to write such a
function more than once, you could easyly use a template function.

HTH
Heinz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top