CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    Derive a class from std::vector

    like i want to derive a class from std::vector and use it with different kinds of objects.

    is it possible?

  2. #2
    Join Date
    Sep 2002
    Location
    land of hurricanes
    Posts
    244
    IT MIGHT BE POSSIBLE IF YOU CAN USE THE VECTOR CLASS ITSELF IF YOU INCLUDE THE VECTOR CLASS ESPECIALLY IF IT HAS VIRTUAL FUNCTIONS, WHAT IS std::vector MATHEMATICAL VECTORS OR WHAT
    We only live once, but we can make many mistakes !

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Possible, yes. Suggested NO. There are a number of good reasons why STL classes should "NEVER" be derived from. I would suggest a search on STL and DERIVE here on the forums to see some of the reasons.

    If you could provide additional information on why you think deriving from vector would be a good idea, we might be able to suggest some approaches that do not have many of the harzard of deriving from an STL template.


    One side note. A good/quick rule of thumb is to look at the descructor of a class, if it is not virtual, then one should generally not (publicly) derive from that class.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Derive a class from std::vector

    Originally posted by IndikaNiva
    like i want to derive a class from std::vector and use it with different kinds of objects.

    is it possible?
    The standard containers are not meant to be derived from, they are meant to be used as components (i.e. members) in any class you create, or just use vector<T> as a standalone variable. The problem with deriving from the standard containers is that there is no virtual destructor for std::vector or any of the standard container classes.

    Since vector is templatized, it can use any type that satisfies the requirements (assignable and copyable), so I don't understand what you mean by "use with different kind of objects". Maybe you should explain in detail what you are trying to do, since there is little, if any reason to be deriving from std::vector.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895
    Ok.. First of all I'm new to template stuff. I have been a hardcore MFC guy, until I did some performance testing with std:vector and CArray. amazing!! inserting 100000 objects took 4 seconds with vector. push_back() and 3 miniutes with CArray.Insert() method.

    So what I need to do is have one common Array class that I can use with different class types. At the same time write some common methods like CArray:: methods to handle insert, delete, getAt, flush etc. I like to keep the CArray method names as much as possible, so the user can just use myClass::Insert(CXyyy* pVariablePointer);

    I know this kind of sounds pointless but, the people that are going to use this calss will like it more if I have CArray:: names for functions, as opposed to std:vecor names.

    May be there is an answer without deriving but using templates, to do this. any ideas are welcome.

    Thanx Paul, CPU and Stoll
    Last edited by IndikaNiva; January 2nd, 2003 at 09:56 PM.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Write the class with the interface you want (aka CArray) and have a vector member that you direct all calls to.

    If you make all of the metohd inline, there should be little or no performance hit.

    Hope this helps.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895
    thanx Wiz! My template knoledge sucks!
    But how do I deal with different kinds of objects in my collection.

    example CMyArray::Insert() should be able to insert one of CX1*, CX2* and CMx* etc... and GetItemAt should be able to return one of any type of pointe. just like CArray<CXX*> will return and deal with CXX*.

    so my CMyArray should be able to deal with one of any type of pointer.

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Have you looked at how CArray is implemented???

    It is a template itself

    I would suggest some studing on templates, there are a good number or articles here on CodeGuru.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895
    OK I feel like an idiot.

    Thats waht I'm doing right now.

    Thanx all.

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

    Question

    To my knowledge, STL vector class doesn't have synchronization for multi-threaded purposes. Out of curiosity, I would like to known if there is anyway I can extend the vector class with synchronization features while maintaining all the vector's methods?

    Any suggestion is appreciated. Thanks!

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    Kheun,

    I fail to see what that has to do with this post

    btw: The wrapping technique is probably the best way to extend thread safety...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Thanks for the quick reply, TheCPUWizard.

    It just happened that one of my friend has create a subclass of vector and extending it with mutex to lock and unlock simultaneous access. Since STL wasn't meant to used as base class, I am wondering if his approach is appropriate. I had also debated with him on the use of the wrapper but failed to convince him.

  13. #13
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    Take a look at

    http://www.boost.org/

    For thread related issue in STL.

    And for inheritance visit

    http://www.gotw.ca/publications/mill06.htm
    http://www.gotw.ca/publications/mill07.htm

    Hope it helps.

  14. #14
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Zeeshan, thank you very much! I will check out the links.

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