Thread: vectors and polymorphism

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    vectors and polymorphism

    Hi,

    I'm having an issue trying to pass a vector to a function.

    The function parameter is of vector<base_class>, then I'm actually passing vector<sub_class>, but it doesn't work.

    Is it possibly to do this in C++?

    Here's more specific code

    Code:
    
    void drawRectangles(vector <Rectangle> v) {
    // do whatever in here
    }
    
    // some other place is calling
    vector <Building> buildings;
    
    // populate that
    drawRectangles(buildings);
    
    
    // Buildings looks like
    
    class Building : public Rectangle ....
    Thanks

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Is drawRectangles a member function? Or a regular function defined outside the scope of a class?

    If it is a member function it needs to be virtual and the parameter must be a reference or pointer to the base class. There is no dynamic binding happening if these two conditions aren't meet.

    If the function is not a member function then you can only use the static type. This means that the argument you are using to pass to the function parameter must be of vector<Rectangle> type. Or... (and this is probably more correct) be of vector<Building> type and the parameter must be changed.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no relationship between a container of one class and a container of another, even if there is a relationship between the two contained classes. Sorry. Make drawRectangles take a container of Rectangle pointers that you fill with pointers to your Buildings.

    Of course, having Building inherit from Rectangle is a design error in the first place.
    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

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Of course, having Building inherit from Rectangle is a design error in the first place.
    I have several different objects (2 for now, but maybe more later), that are basically Rectangles but have certain differences (hence the subclasses). I don't really see how this is a design error, it's OOP, but I'm used to working in Java, haven't done much C++, although I think I may just make everything a rectangle and add the extra attributes as necessary, it may simpify things.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Because a building isn't a rectangle. It is made of them so you would make rectangles a member variable of Building.
    Woop?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Yippee! I am a rectangle! This forum is a rectangle! The world is a rectangle! Get your concepts straight.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. should I build my own vector and matrix classes?
    By Shadow12345 in forum Game Programming
    Replies: 26
    Last Post: 01-13-2003, 08:41 PM