Problem with depracated casting method (down casting)

Discussion in 'C++' started by Wally Barnes, Nov 20, 2008.

  1. Wally Barnes

    Wally Barnes Guest

    Can someone help a poor C++ programmer that learned the language before
    there was a standard lib .. etc ?

    Basically I have two classes that look something like below:


    template <class T>
    class ListLogic {
    public:
    struct LogicBase {
    LogicBase () : next(0) { }
    LogicBase* next;
    };
    protected:
    LogicBase* head ();
    };

    template <class T>
    class AList : private ListLogic {
    protected:
    struct ListNode : public ListLogic::LogicBase {
    ListNode (const T& d) : data(d) { }
    T data;
    };
    public:
    ......
    T& head () { return ((AList<T>::ListNode*)ListLogic::head())->data; }
    };

    The 'AList<T>::head()' function is supposed to return the first item in the
    list by taking the LogicBase pointer returned by the 'ListLogic::head()'
    function and casting it a ListNode struct pointer. Then derefencing it to
    retrieve the 'data' part of the ListNode struct.

    On earlier versions of g++, this compiles and works fine albeit with the
    following warnings:

    "warning: `typename AList<T>::ListNode' is implicitly a typename"
    "warning: implicit typename is deprecated"

    The problem is that on newer versions of g++ the same code now is an error
    with the following verbage:

    "In member function 'T& AList<T>::head()':"
    "error: expected primary-expression before ')' token"
    "error: expected `)' before 'ListLogic'"

    So it seems the old style casting used previously is no longer allowed. What
    is the stardard compliant method of down casting the ListLogic::LogicBase
    pointer to the AList<T>::ListNode pointer ? Thanks in advance for any
    assistance.

    -Wally
    Wally Barnes, Nov 20, 2008
    #1
    1. Advertising

  2. Wally Barnes wrote:
    >
    > So it seems the old style casting used previously is no longer allowed. What
    > is the stardard compliant method of down casting the ListLogic::LogicBase
    > pointer to the AList<T>::ListNode pointer ? Thanks in advance for any
    > assistance.
    >


    The error you are getting has absolutely noting to do with casing per
    se. It is about the way you specify the name of the type. In certain
    contexts type names that depend on the template parameters must be
    preceded with the keyword 'typename'. Use 'typename AList<T>::ListNode*'
    in you cast. However, when it comes to casting pointers along class
    hierarchy, it is always better to use the new C++ casts instead of old
    C-style casts. In your case that would be 'static_cast'.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 20, 2008
    #2
    1. Advertising

  3. Wally Barnes

    Wally Barnes Guest

    Andrey Tarasevich wrote:

    > Wally Barnes wrote:
    >>
    >> So it seems the old style casting used previously is no longer allowed.
    >> What is the stardard compliant method of down casting the
    >> ListLogic::LogicBase pointer to the AList<T>::ListNode pointer ? Thanks
    >> in advance for any assistance.
    >>

    >
    > The error you are getting has absolutely noting to do with casing per
    > se. It is about the way you specify the name of the type. In certain
    > contexts type names that depend on the template parameters must be
    > preceded with the keyword 'typename'. Use 'typename AList<T>::ListNode*'
    > in you cast. However, when it comes to casting pointers along class
    > hierarchy, it is always better to use the new C++ casts instead of old
    > C-style casts. In your case that would be 'static_cast'.
    >


    Andrey,

    Thanks for the response. The issue was resolved tow different ways. The
    first by inserting the 'typename' keyword inside the existing old style
    cast. The other using the 'static_cast<>' new C++ standard cast. Thanks
    again for the update to my outdated mental C++ database.

    -Wally
    Wally Barnes, Nov 20, 2008
    #3
  4. Wally Barnes

    Wally Barnes Guest

    Victor Bazarov wrote:

    > Wally Barnes wrote:
    >> Can someone help a poor C++ programmer that learned the language before
    >> there was a standard lib .. etc ?
    >>
    >> Basically I have two classes that look something like below:
    >>
    >>
    >> template <class T>
    >> class ListLogic {
    >> public:
    >> struct LogicBase {
    >> LogicBase () : next(0) { }
    >> LogicBase* next;
    >> };
    >> protected:
    >> LogicBase* head ();
    >> };
    >>
    >> template <class T>
    >> class AList : private ListLogic {

    >
    > template<class T>
    > class AList : private ListLogic<T> {
    >
    >> protected:
    >> struct ListNode : public ListLogic::LogicBase {

    >
    > struct ListNode : public ListLogic<T>::LogicBase {
    >
    >> ListNode (const T& d) : data(d) { }
    >> T data;
    >> };
    >> public:
    >> ......
    >> T& head () { return ((AList<T>::ListNode*)ListLogic::head())->data; }

    >
    > Maybe
    >
    > T& head() {
    > return static_cast<
    > typename AList::ListNode*
    > >(ListLogic::head())->data;

    > }
    >
    >> };
    >>
    >> The 'AList<T>::head()' function is supposed to return the first item in
    >> the list by taking the LogicBase pointer returned by the
    >> 'ListLogic::head()' function and casting it a ListNode struct pointer.
    >> Then derefencing it to retrieve the 'data' part of the ListNode struct.
    >>
    >> On earlier versions of g++, this compiles and works fine albeit with the
    >> following warnings:
    >>
    >> "warning: `typename AList<T>::ListNode' is implicitly a typename"
    >> "warning: implicit typename is deprecated"
    >>
    >> The problem is that on newer versions of g++ the same code now is an
    >> error with the following verbage:
    >>
    >> "In member function 'T& AList<T>::head()':"
    >> "error: expected primary-expression before ')' token"
    >> "error: expected `)' before 'ListLogic'"
    >>
    >> So it seems the old style casting used previously is no longer allowed.
    >> What is the stardard compliant method of down casting the
    >> ListLogic::LogicBase pointer to the AList<T>::ListNode pointer ? Thanks
    >> in advance for any assistance.

    >
    > I believe a static_cast should do it. And in general, the 'typename' is
    > required, I think.
    >
    > V


    Victor,

    Thanks for the response. The issue was resolved tow different ways. The
    first by inserting the 'typename' keyword inside the existing old style
    cast. The other using the 'static_cast<>' new C++ standard cast. Thanks
    again for the update to my outdated mental C++ database.

    -Wally
    Wally Barnes, Nov 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,782
    Andrew McDonagh
    Jan 8, 2005
  2. Doug Mazzacua

    Depracated HTML Features

    Doug Mazzacua, Dec 3, 2005, in forum: HTML
    Replies:
    2
    Views:
    405
    Jim Higson
    Dec 3, 2005
  3. mead
    Replies:
    7
    Views:
    363
    Leor Zolman
    May 3, 2004
  4. Ingo Nolden
    Replies:
    5
    Views:
    3,863
    Siemel Naran
    Dec 15, 2004
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    547
    James Kanze
    Dec 20, 2009
Loading...

Share This Page