Thread: vectors and polymorphism

  1. #1
    Registered User
    Join Date
    Mar 2003

    vectors and polymorphism


    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

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

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    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
    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!

    "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
    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
    Join Date
    Aug 2002
    Tucson, Arizona
    Because a building isn't a rectangle. It is made of them so you would make rectangles a member variable of Building.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Yippee! I am a rectangle! This forum is a rectangle! The world is a rectangle! Get your concepts straight.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /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