Help getting to a derived class template given a pointer to a no-template base class

Discussion in 'C++' started by chris.kemmerer@att.net, Sep 26, 2007.

  1. Guest

    I am having a problem with templates and I hope someone here can help.

    I am writing a library that accepts data packets, parses them and
    saves the information for later use. One member of the packet is an
    enumeration that says what type of data the packet contains (int,
    char, etc.). I have created classes similar to below. The problem I am
    having is in trying to access the derived class given only a pointer
    to the base class. I know that the pointer I am reading from the
    vector points to the appropriate derived class but I have no way of
    knowing it's underlying data type before hand so I can't explicitly
    declare a variable of the correct derived class. Any help is
    appreciated even if it is a definitive "Can't do it".

    Thanks,

    Chris

    class PacketBase
    {
    virtual ~PacketBase() {}
    ...
    };

    template<typename T>
    class Packet : public PacketBase
    {
    std::vector<T> Values() { return m_Values; }
    std::vector<T> m_Values;
    ...
    };

    class UsePackets
    {
    std::vector<PacketBase*> m_Packets;
    ...
    };

    .... somewhere in the main code...
    UsePackets foo;
    foo.m_Packets.push_back(new Packet<int>);
    foo.m_Packets.push_back(new Packet<short>);
    PacketBase* packet = foo.m_Packets.at(1);
    packet->Values(); // can't access this function
     
    , Sep 26, 2007
    #1
    1. Advertising

  2. wrote:
    > I am having a problem with templates and I hope someone here can help.


    It's not a problem with templates. It's a problem with understanding
    (or not understanding) how inheritance works, I'm afraid.

    > I am writing a library that accepts data packets, parses them and
    > saves the information for later use. One member of the packet is an
    > enumeration that says what type of data the packet contains (int,
    > char, etc.). I have created classes similar to below.


    "Similar"?

    > The problem I am
    > having is in trying to access the derived class given only a pointer
    > to the base class. I know that the pointer I am reading from the
    > vector points to the appropriate derived class but I have no way of
    > knowing it's underlying data type before hand so I can't explicitly
    > declare a variable of the correct derived class. Any help is
    > appreciated even if it is a definitive "Can't do it".
    >
    > Thanks,
    >
    > Chris
    >
    > class PacketBase
    > {
    > virtual ~PacketBase() {}
    > ...
    > };
    >
    > template<typename T>
    > class Packet : public PacketBase
    > {
    > std::vector<T> Values() { return m_Values; }


    Bad idea to return by value. BTW, is this function declared 'private'
    intentionally?

    > std::vector<T> m_Values;
    > ...
    > };
    >
    > class UsePackets
    > {
    > std::vector<PacketBase*> m_Packets;
    > ...
    > };
    >
    > ... somewhere in the main code...
    > UsePackets foo;
    > foo.m_Packets.push_back(new Packet<int>);
    > foo.m_Packets.push_back(new Packet<short>);
    > PacketBase* packet = foo.m_Packets.at(1);
    > packet->Values(); // can't access this function


    What are you trying to do? 'PacketBase' does not have 'Values'
    member. Is that what your compiler is telling you? Well, it is
    correct. Or is it telling you that the member is "unaccessible"?

    Read the FAQ 5.8 and follow its recommendations.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 26, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 26, 4:50 pm, "Victor Bazarov" <> wrote:
    > wrote:
    > > I am having a problem with templates and I hope someone here can help.

    >
    > It's not a problem with templates. It's a problem with understanding
    > (or not understanding) how inheritance works, I'm afraid.
    >
    > > I am writing a library that accepts data packets, parses them and
    > > saves the information for later use. One member of the packet is an
    > > enumeration that says what type of data the packet contains (int,
    > > char, etc.). I have created classes similar to below.

    >
    > "Similar"?
    >
    >
    >
    > > The problem I am
    > > having is in trying to access the derived class given only a pointer
    > > to the base class. I know that the pointer I am reading from the
    > > vector points to the appropriate derived class but I have no way of
    > > knowing it's underlying data type before hand so I can't explicitly
    > > declare a variable of the correct derived class. Any help is
    > > appreciated even if it is a definitive "Can't do it".

    >
    > > Thanks,

    >
    > > Chris

    >
    > > class PacketBase
    > > {
    > > virtual ~PacketBase() {}
    > > ...
    > > };

    >
    > > template<typename T>
    > > class Packet : public PacketBase
    > > {
    > > std::vector<T> Values() { return m_Values; }

    >
    > Bad idea to return by value. BTW, is this function declared 'private'
    > intentionally?
    >
    >
    >
    > > std::vector<T> m_Values;
    > > ...
    > > };

    >
    > > class UsePackets
    > > {
    > > std::vector<PacketBase*> m_Packets;
    > > ...
    > > };

    >
    > > ... somewhere in the main code...
    > > UsePackets foo;
    > > foo.m_Packets.push_back(new Packet<int>);
    > > foo.m_Packets.push_back(new Packet<short>);
    > > PacketBase* packet = foo.m_Packets.at(1);
    > > packet->Values(); // can't access this function

    >
    > What are you trying to do? 'PacketBase' does not have 'Values'
    > member. Is that what your compiler is telling you? Well, it is
    > correct. Or is it telling you that the member is "unaccessible"?
    >
    > Read the FAQ 5.8 and follow its recommendations.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    I'm sorry, I was making up these classes to show the issue with as
    little superfluous stuff as possible. The Values() functions is
    declared public.

    I am currently passing by value but that is not a requirement and I
    can easily change it to pass by reference.

    I know that PacketBase does not have a Values() function because it
    can't, PacketBase doesn't know anything about the template type. I
    could do away with all this indirection if UsePackets could hold a
    vector of Packet class pointers but I didn't think that was possible
    since Packet is a class template and each Packet instance can have a
    different type.

    Where do I find FAQ 5.8?

    Thanks,

    Chris
     
    , Sep 26, 2007
    #3
  4. wrote:
    > [..]
    > Where do I find FAQ 5.8?


    See http://www.parashift.com/c -faq-lite/ And "Where to I find
    the FAQ for this newsgroup?" should be the first question you
    ask when you walk in. Of course, you wouldn't have to do that
    if you bothered to read the newsgroup for at least a day before
    posting...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 26, 2007
    #4
  5. Guest

    On Sep 26, 9:32 pm, wrote:

    >
    > class PacketBase
    > {
    > virtual ~PacketBase() {}
    > ...
    >
    > };
    >
    > template<typename T>
    > class Packet : public PacketBase
    > {
    > std::vector<T> Values() { return m_Values; }
    > std::vector<T> m_Values;
    > ...
    >
    > };
    >
    > class UsePackets
    > {
    > std::vector<PacketBase*> m_Packets;
    > ...
    >
    > };
    >
    > ... somewhere in the main code...
    > UsePackets foo;
    > foo.m_Packets.push_back(new Packet<int>);
    > foo.m_Packets.push_back(new Packet<short>);
    > PacketBase* packet = foo.m_Packets.at(1);
    > packet->Values(); // can't access this function


    Would something like this work?

    class PacketBase
    {
    virtual ~PacketBase() {}
    virtual std::vector<boost::any>& Values() const =0;
    ...

    };

    template<typename T>
    class Packet : public PacketBase
    {
    std::vector<boost::any>& Values() const { return m_Values; }
    std::vector<boost::any> m_Values;
    ...

    };

    class UsePackets
    {
    std::vector<PacketBase*> m_Packets;
    ...

    };

    ... somewhere in the main code...
    UsePackets foo;
    foo.m_Packets.push_back(new Packet<int>);
    foo.m_Packets.push_back(new Packet<short>);
    PacketBase* packet = foo.m_Packets.at(1);
    vector<boost::any> woot = packet->Values();

    You may also be able to wrap the vector itself in boost::any but I've
    never done anything like that.

    Saul
     
    , Sep 27, 2007
    #5
  6. Guest

    On Sep 27, 5:36 am, wrote:
    > On Sep 26, 9:32 pm, wrote:
    >
    >
    >
    >
    >
    >
    >
    > > class PacketBase
    > > {
    > > virtual ~PacketBase() {}
    > > ...

    >
    > > };

    >
    > > template<typename T>
    > > class Packet : public PacketBase
    > > {
    > > std::vector<T> Values() { return m_Values; }
    > > std::vector<T> m_Values;
    > > ...

    >
    > > };

    >
    > > class UsePackets
    > > {
    > > std::vector<PacketBase*> m_Packets;
    > > ...

    >
    > > };

    >
    > > ... somewhere in the main code...
    > > UsePackets foo;
    > > foo.m_Packets.push_back(new Packet<int>);
    > > foo.m_Packets.push_back(new Packet<short>);
    > > PacketBase* packet = foo.m_Packets.at(1);
    > > packet->Values(); // can't access this function

    >
    > Would something like this work?
    >
    > class PacketBase
    > {
    > virtual ~PacketBase() {}
    > virtual std::vector<boost::any>& Values() const =0;
    > ...
    >
    > };
    >
    > template<typename T>
    > class Packet : public PacketBase
    > {
    > std::vector<boost::any>& Values() const { return m_Values; }
    > std::vector<boost::any> m_Values;
    > ...
    >
    > };
    >
    > class UsePackets
    > {
    > std::vector<PacketBase*> m_Packets;
    > ...
    >
    > };
    >
    > ... somewhere in the main code...
    > UsePackets foo;
    > foo.m_Packets.push_back(new Packet<int>);
    > foo.m_Packets.push_back(new Packet<short>);
    > PacketBase* packet = foo.m_Packets.at(1);
    > vector<boost::any> woot = packet->Values();
    >
    > You may also be able to wrap the vector itself in boost::any but I've
    > never done anything like that.
    >
    > Saul- Hide quoted text -
    >
    > - Show quoted text -


    Thank your that idea. I usually don't like to use non-standard
    libraries in order to keep my code as portable, and standards based as
    possible but I recently attended some classes where Boost was featured
    and it looked like it had some very usefull features.

    A related question that may actually solve my first problem. The real
    issue is being able to create a type from the enumeration I get from
    the data packet. If I could somehow morph that into a usable typename
    I could dynamically cast the base pointer to the proper derived
    template. Something of the form:
    typedef typeid(???).name T;
    PacketBase* base = foo.m_Packets.at(1);
    Packet<T>* derived = dynamic_cast<Packet<T>*>(base);
    The problem is ??? isn't an object, just an enumeration.

    Another way is if I can somehow pass the type up from the derived
    class template to the base class.

    In the mean time I'll look into Boost::Any.

    Thank you.
     
    , Sep 27, 2007
    #6
  7. Guest

    On Sep 27, 2:26 pm, wrote:

    >
    > A related question that may actually solve my first problem. The real
    > issue is being able to create a type from the enumeration I get from
    > the data packet. If I could somehow morph that into a usable typename
    > I could dynamically cast the base pointer to the proper derived
    > template. Something of the form:
    > typedef typeid(???).name T;
    > PacketBase* base = foo.m_Packets.at(1);
    > Packet<T>* derived = dynamic_cast<Packet<T>*>(base);
    > The problem is ??? isn't an object, just an enumeration.
    >


    Sorry, I didn't previous realise that the original problem is actually
    that you need to store different types in the same container. Its best
    not to throw away the type information at all. One possible way is
    using vector<boost::variant<int,short,etc> > to hold the packets along
    with boost::static_visitor when you want to process them.

    Saul
     
    , Sep 27, 2007
    #7
  8. Adam Nielsen Guest

    Re: Help getting to a derived class template given a pointer to ano-template base class

    > I know that the pointer I am reading from the
    > vector points to the appropriate derived class but I have no way of
    > knowing it's underlying data type before hand so I can't explicitly
    > declare a variable of the correct derived class.


    Couldn't you use dynamic_cast?

    > UsePackets foo;
    > foo.m_Packets.push_back(new Packet<int>);
    > foo.m_Packets.push_back(new Packet<short>);
    > PacketBase* packet = foo.m_Packets.at(1);
    > packet->Values(); // can't access this function


    Packet<int> *pi = dynamic_cast< Packet<int>* >(packet);
    if (pi) pi->Values();
    else {
    Packet<short> *ps = dynamic_cast< Packet<short>* >(packet);
    if (ps) ps->Values();
    }

    Not the nicest code and there's probably a far better way, but it is a
    quick solution.

    Cheers,
    Adam.
     
    Adam Nielsen, Sep 28, 2007
    #8
    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. Matt Graham
    Replies:
    0
    Views:
    565
    Matt Graham
    Jul 21, 2003
  2. tirath
    Replies:
    3
    Views:
    710
    Ivan Vecerina
    Oct 12, 2003
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    8
    Views:
    959
    gwowen
    Aug 18, 2010
Loading...

Share This Page