0

I have the following: first a string that is not a constant

std::string anId;

an object that is a constant

const Object * object;

This object has a method like

 const std::shared_ptr<RawData> GetRawData(const std::string &);

and there is a function like this

getData(const std::shared_ptr<RawData> raw_ptr);

So, when I do this:

getData(object->GetRawData(anId));

I get the error

error: passing ‘const Object ’ as ‘this’ argument discards qualifiers [-fpermissive]
         int stat = getData(object->GetRawData(anId));

Except the first string, I don't see how I am not passing constants... I wonder where is the error coming from

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • you are not only passing the string. The object itself is also an implicit parameter to the method – 463035818_is_not_an_ai Nov 30 '20 at 11:01
  • @idclev463035818 so which non-constant I am passing to a constant argument? I don't see it – KansaiRobot Nov 30 '20 at 11:06
  • `object` is `const` while `GetRawData` can only be called on a non-constant object. Its explained in the duplicate (see link at the top) – 463035818_is_not_an_ai Nov 30 '20 at 11:08
  • methods should be `const` be default unless they need to modify the object – 463035818_is_not_an_ai Nov 30 '20 at 11:09
  • Other than making the method `const` is there any other way to correct it. The file with `GetRawData` is used in a lot of other parts of the code (it is huge) and I prefer not to break other people's modules... – KansaiRobot Nov 30 '20 at 11:21
  • you will not break code (unless someone is doing weird stuff). You will actually make other code better by doing that change. A `const` method can be called on a `const` object *and* on a non-const object. The other way around is more likely to cause problems. – 463035818_is_not_an_ai Nov 30 '20 at 11:24

0 Answers0