Inheritance and STL

This is a discussion on Inheritance and STL within the C++ Programming forums, part of the General Programming Boards category; I'm running into problems when I try to access a multidemensional array,which is of type stl::vector<stl::vector<controlp>> from a derived class. ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    Inheritance and STL

    I'm running into problems when I try to access a multidemensional array,which is of type stl::vector<stl::vector<controlp>> from a derived class. I've temporarily made the superclass's datamembers public to rule out any problems with data hiding from inheritance.

    A derived object crashes with "acces violation" when it tries to perform:

    get_no_rows();

    which is equivalent to

    cpoints.size();

    If I declare an object of the base class everything works just fine. It's just the derived object that fails.

    I'm very thankful for any advice


    the derived class looks like this:
    Code:
    class Bsurface : public surface 
    {
    public:
        Bsurface(){}
    	Bsurface(int rows, int cols){surface(rows,cols);}
    
    	void render_surface();
    
    	friend void render_Patch(patch& org, int target_depth);
    };
    and the base class looks something like this:
    Code:
    class surface
    {
    public:
    
        surface(){surface(4,4);}
        surface(int rows, int cols);
    	
    	void render_surface_grid();
    	void render_cpoints();
    
    	int get_no_rows();
    	
    	int get_no_cols();
        
        // other ctors ....
        std::vector<controlp> & operator[](int i)
        {
            return cpoints[i];
        }
        const std::vector<controlp> & operator[] (int i) const
        {
            return cpoints[i];
        }
      
    	void debug_print();
    
    	friend void render_Patch(patch& org, int target_depth);
    public:
        std::vector<std::vector<controlp> > cpoints;
    };

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not sure about the access violation (what does the stack say?), but I don't think you should be calling the second constructor like that. In the derived class, simply add the base class constructor to the initialization list. In the base class, you should make a separate init function to initialize and move the code that is in surface(int rows, int cols) to the init function.
    Code:
    class Bsurface : public surface 
    {
    public:
        Bsurface(){}
        Bsurface(int rows, int cols) : surface(rows,cols) {}
        virtual ~Bsurface() {}
    // ...
    };
    
    class surface
    {
    public:
    
        surface(){init(4,4);}
        surface(int rows, int cols) { init(rows, cols); }
        void init(int rows, int cols); // implemented elsewhere
        virtual ~surface() {}
    // ...
    };
    Also, if you are inheriting Bsurface from surface, then you should have a virtual destructor in the base class even if it does nothing.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    Talking

    Wow! It works

    I don't really understand why but I'm going to pick the code apart to see why. Maybe the destructors makes all the difference?

    Thank you very much

    Edit:
    It seems to be because of the constructors and the init thing you mentioned, I'll have to read more to find out why. Thank you again for pointing it out.
    Last edited by Hubas; 10-29-2003 at 07:14 PM.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, this:

    Code:
    Bsurface(int rows, int cols){surface(rows,cols);}
    is not doing what you think it is. It is creating a new, temporary object of the base class which instantly dies.

    You really mean:

    Code:
    Bsurface(int rows, int cols) : surface(rows,cols)
    {}
    which is ENTIRELY different, and properly calls the parent constructor for the object which is being created.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    32
    OK, I can only call superclass constructors through initiatialisaiton lists. Thanks for alll your help guys

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Not sure if you got this but the other point is that even in a single class you can't call one constructor from another (or from any other function for that matter). That is what the init change was for.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    32
    I miss matlab

    But things are beginning to fall into place for me now

    I still have some problems but I'm trying to read up on them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance from STL container
    By SpaceCadet in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2006, 02:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21