I'm clearly confused when it comes to "const" and member pointers.

See the little code snippet below. If I compile it, I get the error:

cxx: Error: trial.C, line 23: (that is, in the const_method)
the object has type qualifiers that are not compatible with the member 
function
        func(ob());

Can someone explain why this is so?

More to the point, what is the difference between ob_ and ob() here?
If I change ob() to 
        FL_OBJECT * ob() const { return ob_; }
then the compiler is happy.

Why am I allowed to pass ob_ to a function that can alter it? This means that 
I can make all the methods of my wrapper class const even though these 
methods are changing *ob_. This smacks of the ridiculous.

I can see what is happening. In the const method, ob_ is implicitly cast to
        FL_OBJECT * const ob_;

I guess I'm asking, why isn't it converted to
        FL_OBJECT const * const ob_;

Angus


struct FL_OBJECT
{
        int dummy;
};

// func() takes a non-const FL_OBJECT *.
void func(FL_OBJECT *) {}

struct Wrapper
{
        void const_method() const;
        void method();
        FL_OBJECT * ob() { return ob_; }
        FL_OBJECT * ob_;
};


void Wrapper::const_method() const
{
        // Valid
        func(ob_);
        // Invalid!
        func(ob());
}

void Wrapper::method()
{
        // Valid
        func(ob_);
        // Also valid
        func(ob());
}

Reply via email to