Looking for advice on passing an object into a function.

I'm looking to pass an object (called Orthography) into a function so I can get at the wstring m_Name variable it has. Much of the advice I've been given on this forum has centered around using const and & when passing something into a function. So I'm looking to pass an Orthography object into a function via "const Orthography&". But when I go that route, I get a red line under my object name and when I hover over it, it says "the object type has qualifiers that are not compatible with the member function." When I Google that error, what I've found refers to using const but I haven't found anything that I can apply to my situation.

Here is my function:

vector<wstring> Parser::CustomOrthography(const wstring& wstrInput, const Orthography& orthography)

And a line in that function that gives the red line is:

m_Results[i] += orthography.GetCharacterMatches()[j].GetOutput();

I get a red line under "orthography" in that line. I'm not sure what is going on or how to approach this. The red line disappears if I don't use const and & but from what I've been reading, it is good practice to use them.
This is a const correctness issue.

'orthography' is a const reference. By being const, it means that you cannot modify it. The compiler does not know that 'GetCharacterMatches' will not modify the object, therefore it gives you that error.

You need to make that function const to tell the compiler it's okay to call it with const objects. You do this by putting a const after the function parameter list:

whatever_type Orthogrophy::GetCharacterMatches() const // <- the const here makes it a const member function

It is good practice to do this with all member functions that do not make any changes to the object. IE: all getters. (unless you have a good reason not to -- which is sometimes the case)
It is good to know that I am starting to pick up some of the "best practices" in programming then :). I had to add "const" to an underlying class but it seems to be working now.

You said putting "const" after the function parameter list tells the compiler that it is ok to call it with const objects. Is that different than putting "const" before the function name?

Thanks for the help!
Topic archived. No new replies allowed.