Accessing non const method from const object

calling non const function from const object
pass non-const to const function
c++ const function
how to protect a class function from modifying this
c++ const function parameter
c++ return const pointer
this' argument to member function const
calling non const function from const function

I have a C++ example where I try to set a property on a class member which is accessed through a const getter.

#include <iostream>

struct prop
    prop() : m_val {0} {}
    int val() const { return m_val; }
    void set_val(int v) { m_val = v; }
    int m_val;

struct base
    virtual ~base() = default;
    virtual prop property() const = 0;
    virtual void set_property(prop p) = 0;

struct derived : public base
    derived() : m_prop {prop {}} {}
    prop property() const { return m_prop; }
    void set_property(prop p) { m_prop = p; }
    prop m_prop;

int main()
    base *d1 = new derived();
    // prints 0
    std::cout << "property val: " << d1->property().val() << '\n';
    delete d1;

    base *d2 = new derived();
    auto p = d2->property();
    // prints 2
    std::cout << "property val: " << d2->property().val() << '\n';
    delete d2;

    return 0;

I would expect this to give me an error on d1->property().set_val(2); because property() method is const and should give me a const prop object which doesn't have set_val method. But this compiles without any errors and the line d1->property().set_val(2); doesn't actually change the val as shown by the cout line. Can someone please explain what I'm missing?

Edit: I now understand the issue with property() returning non-const and by value and why that doesn't give a compiler error and doesn't changed1. The best solution in my use case was to make the return value of property to be prop const&.

d1->property().set_val(2); doesn't actually change the val

Because property() returns by value, that means what property() returns is a temporary object which is copied from the data member m_prop. Then set_val is called on the temporary object which is destroyed immediately.

because property() method is const and should give me a const prop object

No. You declare the return type as prop, then it'll return a non-const prop. In the const property(), the data member m_prop becomes const, it's copied to the returned object which is non-const. The return type won't become to const.

I think you should change the return type to const prop& (in both base and derived class). Then you'll get the error expected. e.g.

const prop& property() const { return m_prop; }

Const function calling non const or vice versa (to avoid duplication , If you have to make a function that is const-agnostic, and avoids duplication, one neat way to do it is delegating implementation to a template, for example void otherClassFunction ( const someClass* theClass ) // also (someClass* const theClass ) - The objective is to tell the compiler the pointer 'theClass' is never to be modified by this method. {. int anInt = theClass.getSomePrivateInt () // produces compiler error "call non-const method for constant object".

property returns a copy of m_prop, which is a modifiable, albeit temporary, value. Because of that, you are not making any changes to dl and the value when printed out will still be the original value.

Const Correctness, C++ FAQ, don't return pointers or references to non-const objects from const methods. physical constness: calling this method does not change the bits in this object. only one const_cast. const-version of the method get () returns a copy. the non-const method gets the non-const reference directly. I don't like method A because: the non-const method get () is const only by contract, (not checked by compiler) harder to get a const-reference, though not impossible.

If you want your property() member function to return a const prop object, specify that it does so:

struct base{
    virtual const prop property() const=0;

As it stands, you get a non-const prop object which is a copy of the property. You can merrily set the values of that object but it won't affect the stored property value.

Using Const Correctly, Once you have a const object, it cannot be assigned to a non-const reference or use functions that are known to be capable of changing the state of the object. This� The const version of getValue() will work with either const or non-const objects, but returns a const reference, to ensure we can’t modify the const object’s data. This works because the const-ness of the function is considered part of the function’s signature, so a const and non-const function which differ only in const-ness are

That the method is const just means that calling it won't mutate the object you call it on.

The object you get back from the method (by value) is a brand new object and you can do to it what you want. It's in no way related to the object you got it from (except from being a copy of what the getter returned) or the constness of the function you called to obtain it.

Const Correctness - C++ Tutorials, duplicate getter methods with the same name (one const, one non-const). This basically means that, if you have a const Page object, you will be able to The interface is a lie; the client does not need to ask for write access to an object, � In non-object-oriented code, this is as easy as using const references as demonstrated above. In C++, however, there's the issue of classes with methods. If you have a const object, you don't want to call methods that can change the object, so you need a way of letting the compiler know which methods can be safely called.

Thoughts on duplicate const/non-const getters, Once initialized, a const data member may never be modified, not even in the constructor or destructor. Data members that are both static and const have their � The only difference between them is that one method is non-const and return a non-const reference (that can be use to modify object) and the second is const and returns const reference. To avoid the code duplication, there is a temptation to call one method from another. However, we can not call non-const method from the const one.

const Data Members, const Objects, and const Methods, In this compliant solution, the function remove_spaces() is passed a non- const char pointer. The calling function must ensure that the null-terminated byte string � It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object. Non-const functions can be called by non-const objects only. Here is the syntax of const member function in C++ language, datatype function_name const(); Here is an example of const member function in C++,

EXP05-C. Do not cast away a const qualification, We can declare a class member function to be const which means that calling it for an object will not change the object's state (i.e. data members). The compiler� The first one with no const allows the caller to modify the object, which is in general a member of the class whose method is being called. The second one, where our host class is in read-only mode, also allows read-only access to its member. By the default, the non-const version is invoked if it is allowed under the rules of constness.