Thread: vector template ?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    317

    vector template ?

    What does the vector class do if you give it a NULL pointer. Does it still add that to the array? And if so you can derive from this class correct?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I think you mean void pointer (I could be wrong). As in:

    vector<void *> v;

    this basically allocates an array of void *s Nothing wrong with that. but if you meant:

    vector<void> v;

    I have no idea what to tell you. Another possibility (since you said NULL pointer):

    vector<SomeClass *> v;
    v.push_back(NULL);

    This is just fine as NULL is valid to store in a SomeClass *.

    Since the second choice seems the wackiest I'm sure that's it. vector<void> v; looks quite pointless but the best I can say is try it out in the compiler if you really want to know. I'm too lazy to test it myself right now.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    Sorry, I'll try to be more specific. In the program I'm using I utilize the vector class and in some instances my algorithm will return a NULL pointer if it does not intend to generate an object at the time. However the way it works is
    Code:
    vector<SomeClass*> SC;
    
    SC = blah();
    
    blah(){
    
         if (somecondition){
                      return new(SomeClass());
         }else
                return NULL;
    }
    I was wondering if this was taking up space in my vector array if blah returned NULL. Also if someone knows another way to do this, suggestions are always welcome.

  4. #4
    Shadow12345
    Guest
    I would say that yes it does
    Code:
    #include <vector>
    #include <iostream>
    using namespace std;
    
    class Poop{
    public:
    	Poop() {}
    private:
    };
    
    vector<Poop*> SC;
    
    int main(void) {
    	SC.push_back(NULL);
    	cout << "SC[0] takes up " << sizeof(SC[0]) << " bytes" << endl;
    	return 0;
    }
    Output:
    The size of SC[0] is 4 bytes

    EDIT:
    I thought the size of a class was supposed to be all of its methods and variables added together. I changed the Poop class to this:
    class Poop{
    public:
    Poop() {}
    int GetHowManyPoops(){return HowManyPoops;}
    private:
    static int HowManyPoops;
    long HowPoops;
    double PoopsManyHow;
    short ManyPoops;
    double Crap;
    double Cheese;
    double ScrazzleFrazzle;
    };

    ...BUT IT STILL SAYS IT IS ONLY 4 BYTES...THAT ISN'T CORRECT!
    Last edited by Shadow12345; 09-02-2002 at 08:53 AM.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You are getting the size of the address of the class, not the actual class. To get the size of the class, do sizeof(Poop).

Popular pages Recent additions subscribe to a feed