• Advertisement

Archived

This topic is now archived and is closed to further replies.

Problem with passing references of private members

This topic is 5500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, I essentially have the following code. To draw the model, drawModel() needs acess to the Vertex data and the Face data. So i created functions in Model that pass a reference to them. However, the two lines in drawModel() give an error: passing `const Model'' as `this'' argument of `const class vector > & Model::getVertices()'' discards qualifiers and the same error again for getFaces(). (the same error occurs when I use pointers instead of references) What is so wrong with calling member functions of a const model? How could I fix this? Or is there any way I could design this better? Thanks for your help.
  
class Model
{
  private:
    vector<Vertex> vertices;
    vector<Face> faces;
  public:
    const vector<Vertex>& getVertices(void)
    {
      return vertices;
    }
    const vector<Face>& getFaces(void)
    {
      return faces;
    }
};

void SdlGLGfxSystem::drawModel(const Model& model)
{
  const vector<Vertex>& vertices = model.getVertices();
  const vector<Face>& faces = model.getFaces();
}
  

"George W. Bush Geography Simplification Initiative"
More info on George W. Bush My Homepage (C++ SDL OpenGL Game Programming)

Share this post


Link to post
Share on other sites
Advertisement
Only const methods can be invoked on a const object ('model' in this case). So make those two getter-methods const like this:

      
const vector<Vertex>& getVertices() const //note const here. Also, 'void' was useless

{
return vertices;
}

const vector<Face>& getFaces() const
{
return faces;
}
Naturally const methods can't change the member variables, unless the member variables are declared as 'mutable'. In this case it doesn't matter. Google for more.


[edited by - civguy on January 31, 2003 4:15:49 PM]

Share this post


Link to post
Share on other sites
I remember to have read about that. I guess it''s one of those things you can only remember if you need them... thank you.

I know that putting void in an empty arg list is now useless in C++, but I still leave it there as a habit. It doesn''t do any harm either, does it.


"George W. Bush Geography Simplification Initiative"

More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)

Share this post


Link to post
Share on other sites

  • Advertisement