|
-
March 13th, 2005, 02:41 AM
#1
Error when trying to dynamic_cast<> a reference of a variable
Hi, I am trying to create a function that compare's two objects.
Here, take a look:
Code:
int Integer::compareTo(const NodeItem &anotherNodeItem)
{
int returnCode = 0;
Integer *otherInteger = dynamic_cast<Integer&>(anotherNodeItem);
if (otherInteger == NULL)
returnCode = ERROR_INCOMPATIBLE_TYPES;
else
returnCode = compareTo(*otherInteger);
delete otherInteger;
return returnCode;
}
The error I get when I try to compile is at this statement:
Code:
Integer *otherInteger = dynamic_cast<Integer&>(anotherNodeItem);
The error is this:
Integer.cpp(54): error C2682: cannot use dynamic_cast to convert from 'const NodeItem' to 'Integer &'
I'm not too sure of another way of doing this, but basically I want the function to be able to take a NodeItem pointer, and check whether the NodeItem is an Integer by using dynamic_cast and testing for a NULL result.
Thanks for the help in advance.
Last edited by johnnyICON; March 13th, 2005 at 05:33 PM.
-
March 13th, 2005, 04:02 AM
#2
Re: Error when trying to dynamic_cast<> a reference of a variable
First
Code:
Integer *otherInteger = dynamic_cast<Integer&>(anotherNodeItem);
'&' should be '*'
Second, please tell what's the relation between NodeItem and Integer types?
-
March 13th, 2005, 05:31 PM
#3
Re: Error when trying to dynamic_cast<> a reference of a variable
An Integer is a NodeItem.
A NodeItem is an abstract class for any piece of data that is in a node.
Last edited by johnnyICON; March 13th, 2005 at 05:35 PM.
-
March 13th, 2005, 05:34 PM
#4
Re: Error when trying to dynamic_cast<> a reference of a variable
Code:
Integer *otherInteger = dynamic_cast<Integer*>(anotherNodeItem);
error C2682: cannot use dynamic_cast to convert from 'const NodeItem' to 'Integer *'
-
March 13th, 2005, 05:37 PM
#5
Re: Error when trying to dynamic_cast<> a reference of a variable
Point 1: you can't remove constness with dynamic_cast: you will need to cast to const Integer&
Point 2: you're trying to assign it to a pointer to integer. Either change the variable declaration to const Integer& or change the cast to dynamic_cast<const Integer *>(&anotherNodeItem) (and make the variable const).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 13th, 2005, 07:26 PM
#6
Re: Error when trying to dynamic_cast<> a reference of a variable
Thanks, it worked.
Code:
const Integer *otherInteger = dynamic_cast<const Integer*>(&anotherNodeItem);
-
March 13th, 2005, 10:52 PM
#7
Re: Error when trying to dynamic_cast<> a reference of a variable
Something I didn't notice, but there is a warning message with that comes with that statment:
Code:
warning C4541: 'dynamic_cast' used on polymorphic type 'NodeItem' with /GR-; unpredictable behavior may result
I'm just wondering if it means anything other than what I can understand as unpredictable behaviour.
-
March 14th, 2005, 12:07 AM
#8
Re: Error when trying to dynamic_cast<> a reference of a variable
See MSDN. You have to turn on RTTI in order to use dynamic_cast<>.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|