CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2004
    Posts
    21

    Question 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.

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Oct 2004
    Posts
    21

    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.

  4. #4
    Join Date
    Oct 2004
    Posts
    21

    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 *'

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  6. #6
    Join Date
    Oct 2004
    Posts
    21

    Re: Error when trying to dynamic_cast<> a reference of a variable

    Thanks, it worked.

    Code:
    const Integer *otherInteger = dynamic_cast<const Integer*>(&anotherNodeItem);

  7. #7
    Join Date
    Oct 2004
    Posts
    21

    Question 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.

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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
  •  





Click Here to Expand Forum to Full Width

Featured