-7

Why am i getting a error when i try to static cast a element* to an int

typedef Element* ElementPtr

int Element::getVP (ElementPtr item)
{
return static_cast <int>(item); // I have a class called Element
}
Computernerd
  • 7,378
  • 18
  • 66
  • 95

2 Answers2

2

Not really sure what's your question, but I feel you want implicit conversion function. To convert Element to int, you want operator int()

struct Element
{
  operator int()
  {
    return i;
  }
  int i;
};

int Element::getVP (Element* item)
{
  return (*item); // I have a class called Element
}

But it's still not clear why you need getVP in Element class.

It' just to show you how to convert struct/class to int type. I'll delete my answer if it's not what you want.

billz
  • 44,644
  • 9
  • 83
  • 100
1

Assuming you're trying to get a int from the Element and you have already overloaded the cast operator of the Element class like so:

operator int() { return m_some_int; }

you can use (assuming item is a pointer)

return (int)(*item);

Instead, if you're trying to get the address of the pointer you can do the following:

std::size_t Element::get_address(Element* item)
{
    return reinterpret_cast<std::size_t>(item);
}
Edward A
  • 2,291
  • 2
  • 18
  • 31