473,396 Members | 2,052 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,396 software developers and data experts.

Problems with tertiary operator and reinterpret_cast

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.

--
Regards,
Taran

Jul 25 '06 #1
2 2402
"Taran" <ta************@honeywell.comschrieb im Newsbeitrag
news:11**********************@p79g2000cwp.googlegr oups.com...
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

Jul 25 '06 #2
Taran posted:
is_class_A? \

Get rid of the north-west slash -- it's redundant, and only serves to uglify
the code.

--

Frederick Gotham
Jul 25 '06 #3

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

Similar topics

22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
39
by: Frederick Gotham | last post by:
It is possible and permissible on a given system, that the alignment requirements of a type are less than the size of that type. For instance, here's a hypothetical set-up: int is 4 bytes, but...
10
by: Andrew Robinson | last post by:
I don't know if this is a compiler error or not. It all kind of makes sense but you would think that the compiler would check the types of the possible outcomes of the tertiary operator (c and d)...
1
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post by:
In C++ we can not overload the explicit typecast operator like static_cast, dynamic_cast, const_cast and reinterpret_cast. The only we can do is the implicit typecast operator like operator...
8
by: Tyno Gendo | last post by:
I have just written a line of code like this (yes, many people think this stinks bad): <?php true == isset($page_seq) ? echo "$page_seq" : false; ?> However it breaks with 'Unexpected T_ECHO'...
6
by: Angel Tsankov | last post by:
How can an overloaded operator& take the address of its argument: template<typename T> Smth operator &(T& SomeObject) { // The address of SomeObject is needed here }
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
1
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I can recall a little trick for getting the address of an object and bypassing any user-defined overload of the addressof operator. I think it was something like: T *p = reinterpret_cast<T*>(...
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.