2

I wanted to understand how the inline member variable work while accessing it through a const member variable. Each time I try doing so, I get an error!

This is what I am trying

#include <iostream>
#include <string>
using namespace std;

class A{
    public:
    A()
    {
        _uuid = 0;
    }
    ~A();
    void setUUID(int n) { _uuid = n; }
    inline int getUUID(){  return _uuid;} const
    int getUUID1() const {  return _uuid;} 
    int getUUIDsmart()const 
    { 
        return _uuid; 
    }
    private:
    int _uuid;    
};

class B {
    public:
    B(){}
    ~B();
    void fun1(const A *obj) 
    {
        cout<<obj->getUUIDsmart()<<endl;   //works fine
        cout<<obj->getUUID1()<<endl;       //works fine
        cout<<obj->getUUID()<<endl;        //error

        A *obj1 = const_cast<A *>(obj);
        cout<<obj1->getUUID()<<endl;       //works fine
    }
};

int main()
{
  B *b = new B;
  A *a = new A;
  a->setUUID(12);
  b->fun1(a);
}

I am able to get my code work through

const_cast

But I am interested in knowing why do i get an error in the inline function if I try accessing it through a const member function?

Update: FIX

My bad. I had the placement of const messed up! Thanks to @bruno

inline int getUUID() const { return _uuid; }
//correct syntax. i placed the const at the end
skuila
  • 23
  • 3

1 Answers1

3

[note : I use the first version of the question]

you place wrongly your const :

inline int getUUID(){  return _uuid;} const
int getUUID1(){  return _uuid;} const
int getUUIDsmart()const 

is in fact

inline int getUUID(){  return _uuid;} 
const int getUUID1(){  return _uuid;} 
const int getUUIDsmart()const 

I just moved the const on the right line for readability reason

You wanted

inline int getUUID() const {  return _uuid;}
int getUUID1() const{  return _uuid;}

in your version none of getUUID1 nor getUUID are const so you cannot apply them on a const instance

There is no link at all with the fact your methods are inline or not.


Note :

cout<<obj->getUUID1()<<endl;       //works fine

it doesn't

bruno
  • 32,421
  • 7
  • 25
  • 37
  • you are correct! But the code with //error interests me more. This is just a instance of my actual code. I'll correct the point observed. – skuila Feb 22 '19 at 18:27
  • @skuila did you read my answer ? : _in your version none of getUUID1 nor getUUID are const so you cannot apply them on a const instance_ – bruno Feb 22 '19 at 18:36
  • it is a link to an online c++ compiler i am using to check the output with code changes quickly you can access the url too cpp.sh/47ei62 – skuila Feb 22 '19 at 18:59
  • @skuila you do not used the same definition, on it you use `int getUUID1() const { return _uuid;} ` so it is _const_, this is not the case in your question ... before you edit it – bruno Feb 22 '19 at 19:01
  • I'm not sure that i was able to express my question clearly. I made a blunder in writing the declaration for getUUID1() but I want to know why getUUID() doesn't work? I'll edit my question appropriately but (no offense) your answer still doesn't explain the why can't I call an inline function in a const member variable ! – skuila Feb 22 '19 at 19:10
  • @skuila ? I already said you several times _getUUID_ is **not** _const_, it is `inline int getUUID(){ return _uuid;}` rather than `inline int getUUID() const { return _uuid;}`, because of that you cannot apply it on a const instance like _obj_ is – bruno Feb 22 '19 at 19:21
  • I got your point its the syntax! thanks! :) sorry for the trouble! – skuila Feb 22 '19 at 19:21