Thread: vector derived class, help

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    vector derived class, help

    Hi!

    I am going to make a vector derived class

    Code:
    class flagvec: public vector<bool> {
    public:
    	//Constructors
    	flagvec(unsigned int);
    };
    It is gonna take the ones and zeroes from the unsigned int and extract them to flags. The last one in the variable will be the last element in the flagvec.

    Now I'm not sure how the vector part of my class is going to behave when I use my own constructor. If a constructor that I have declared is called, how is the vector initiated? How long will it be, or isn't it initiated at all?

    Here's my constructor:

    Code:
    flagvec::flagvec(unsigned int a) {
    	//Here I want the vector to get a start length at 0, but I'm not sure how to call it's constructor
    	while (a) {
    		push_back(a & 1);
    		a >>= 1;
    	}
    }
    Thanks!
    Come on, you can do it! b( ~_')

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You are aware that there may be issues with deriving from the standard containers? http://cboard.cprogramming.com/showthread.php?t=94187

    If you don't call any vector constructor specifically, then the default constructor will be used, which will initialize the vector with a length of zero, so your code should work.

    But why are you creating a whole new type just for a different constructor?

    It seems like you're trying to do something similar to a bitset: http://www.cppreference.com/cppbitse...structors.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by dwks View Post
    You are aware that there may be issues with deriving from the standard containers?
    Is it better to use an internal vector variable instead of having the class derived?

    Quote Originally Posted by dwks View Post
    If you don't call any vector constructor specifically, then the default constructor will be used, which will initialize the vector with a length of zero, so your code should work.
    How do I call a constructor? Calling vector<unsigned int>(...);?

    Quote Originally Posted by dwks View Post
    But why are you creating a whole new type just for a different constructor?
    Well, it was just an example. Actually, my vector derived class is supposed to do something completely else. ^_^
    Come on, you can do it! b( ~_')

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Is it better to use an internal vector variable instead of having the class derived?
    Yes. Making it a member is usually the way to go.

    >> How do I call a constructor?
    Code:
    vector<int> myvec; // no parentheses when using the default constructor.
    BTW, if you're actually using vector<bool>, you might consider std::bitset instead, depending on your real requirements.

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs up

    Quote Originally Posted by Daved View Post
    >> Is it better to use an internal vector variable instead of having the class derived?
    Yes. Making it a member is usually the way to go.

    >> How do I call a constructor?
    Code:
    vector<int> myvec; // no parentheses when using the default constructor.
    BTW, if you're actually using vector<bool>, you might consider std::bitset instead, depending on your real requirements.
    Thanks!
    Come on, you can do it! b( ~_')

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm not sure if that is something you are trying to achieve but here's something I threw together out of boredom. I just used as an unsigned as storage because this is probably most versatile. For example, would bitset or vector<bool> let you test combinations of patterns (any_of, all_of) - without a loop?

    Code:
    #ifndef BINFLAGS_H
    #define BINFLAGS_H
    
    #include <limits>
    
    /*
        BinFlags represents a collection of options together with
        some basic functionality to test and set/reset options.
    
        Template argument must be an enumeration where the largest value
        does not exceed number of bits in unsigned.
    */
    template <typename Enum>
    class BinFlags
    {
        public:
            BinFlags(): state(0) {}
            void set(Enum n)
            {
                state |= (1 << n);
            }
            void reset(Enum n)
            {
                state &= (std::numeric_limits<unsigned>::max() ^ ( 1 << n));
            }
            void toggle(Enum n)
            {
                state ^= (1 << n);
            }
            bool is_set(Enum n) const
            {
                return state & (1 << n);
            }
            bool any_of(const BinFlags flags) const
            {
                return state & flags.state;
            }
            bool all_of(const BinFlags flags) const
            {
                return state & flags.state == flags.state;
            }
        private:
            unsigned state;
    };
    
    #endif
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't use vector<bool>. It's an ill-conceived abomination. Use std::bitset if you know how many bits you need (the typical case), and Boost.DynamicBitset if you don't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Another thing, I have made a constructor for the class which translates an integer into the vector. But I also want to be able to set an already existing object of this class to a value by typecasting, do I have to overload the = operator to do the exactly same thing as the constructor? Or can I somehow use the = operator in the constructor to save code?

    So,

    Code:
    //External constructor for flagvec from an unsigned int
    flagvec::flagvec(unsigned int a) {
    	operator=(a);
    }
    You se what I mean? Is this right?
    Come on, you can do it! b( ~_')

  9. #9
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    >> Don't use vector<bool>

    Once again, this is only an example. I should never use vector<bool> for real!
    Come on, you can do it! b( ~_')

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You should avoid using vector<bool> whether it's a base class or not. You can read Iten 18 of "Effective STL" by Scott Meyers for more info, but here are some notable quotes:
    there are really only two things wrong with vector<bool>. First, it's not an STL container. Second, it doesn't hold bools.
    ...
    An object becomes an STL container only if it fulfills all the container requirements laid down in section 23.1 of the Standard for C++. Among the requirements is that if c is a container of objects of type T and c supports operator[], the following must compile:
    T *p = &c[0];
    ...
    vector<bool> is a peudo-container that contains not actual bools, but a packed representation of bools that is designed to save space.

  11. #11
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by cpjust View Post
    You should avoid using vector<bool> whether it's a base class or not. You can read Iten 18 of "Effective STL" by Scott Meyers for more info, but here are some notable quotes:
    So you mean that vector<bool> becomes smaller than expected, almosed as if it would have been compressed? Does it have anything with the definition of bool to do?
    Come on, you can do it! b( ~_')

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It stores the bools as bits to save space. Because it does that, it breaks some other rules that work for all other vectors.

    If you expect it to work like every other vector (especially if you expect it to be convertible to bool*), then don't use it.

    Of course, if you're not counting on the fact that actual bools are stored, then I don't see anything wrong with using it.

  13. #13
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Daved View Post
    It stores the bools as bits to save space. Because it does that, it breaks some other rules that work for all other vectors.

    If you expect it to work like every other vector (especially if you expect it to be convertible to bool*), then don't use it.

    Of course, if you're not counting on the fact that actual bools are stored, then I don't see anything wrong with using it.
    Wow, i actually didn't know of that ...
    Come on, you can do it! b( ~_')

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by CornedBee View Post
    Don't use vector<bool>. It's an ill-conceived abomination. Use std::bitset if you know how many bits you need (the typical case), and Boost.DynamicBitset if you don't.
    Is the equivalent functionality of vector<bool> going to be provided in a proper way in C++09?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, C++09 isn't going to remove vector<bool>. I'm not even sure if they accept the proposals to deprecate it.

    I haven't heard of a replacement yet. Consider, however, this: the need for dynamic bitsets is actually quite rare. You can always use dynamic_bitset from Boost, though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM