-
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;
};
-
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.
-
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.
-
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.
-
OK, I can only call superclass constructors through initiatialisaiton lists. Thanks for alll your help guys:)
-
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.
-
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.