• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Accessing inline function of a class through const pointer object.

Hi all
i hav a class say ABC. ABC has an inline func
inline bool ABC::rucrazy()
{
return m.crazy;
}

Now i want to access this inline fucntion.
If i have ABC &object, then object.rucrazy() works.
but if i have const ABC *object, then i m unable to get the
value of crazy.
It say "Cannot convert from const ABC to ABC &".
is there any way out?
(its kinda urgent.so pz try 2 b quick :)))

Thanks
Jastej.
0
jastiwana
Asked:
jastiwana
  • 2
1 Solution
 
furqanchandioCommented:
hi

the problem i see is that the pointer ABC* object needs to be assigned an address of an object or allocated using new.
if u have try using object->runcrazy(); ( object is a pointer afterall)


OR

maybe ur runcrazy function is modifying data therefore it cant be assigned const


kindly show the code of the runcrazy function. is this the complete code of runcrazy?
0
 
novitiateCommented:
const ABC *object = new ABC();
const_cast<ABC*>(object)->rucrazy();

_novi_
0
 
novitiateCommented:
or you may
class ABC
{
public:
      bool rucrazy() const;
};

inline bool ABC::rucrazy() const
{
      bool crazy = true;
      return crazy;
}

const ABC *object;
since the object has been declared as constant, it can't invoke non const functions, as they may modify the object.

so either you should declare you function const or remove the const by using const_cast

_novi_

_novi_
0
 
balderCommented:

inline bool ABC::rucrazy()
{
return m.crazy;
}

should be
 
inline bool ABC::rucrazy() const
{
return m.crazy;
}

but since you don't explain what m is there might be further problems with that.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now