0

I'm writing a code with following function and new to C++

foo(void const *pCluster, const uint32_t fieldNum, __attribute__((unused)) const uint32_t subFieldNum, const int64_t value)
{
    bool status = false;

    mycustomStruct* const pFlmcCtrl = static_cast<mycustomStruct* const>(pMdbCluster);

   // Some Processing
}

This gives error error: static_cast from 'const void *' to 'mycustomStruct* const' casts away qualifiers.

Please help me understand the error here. I could not understand

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
T T
  • 144
  • 1
  • 3
  • 17

1 Answers1

1

This

T const* p

is a pointer to a const T. This

T* const p

is a const pointer to a T. Notice the difference? In the first example, the thing the pointer points to is the thing that is const. In the second example, the pointer itself is the thing that is const. Attempting to cast a void const* to a mycustomStruct* const means casting a pointer that points to something that is const to a pointer that points to something that is not const (just the pointer itself happens to be const). Thus, this cast would drop a const qualifier, which is not something that static_cast can do…

You probably wanted to write

const mycustomStruct* pFlmcCtrl = static_cast<const mycustomStruct*>(pMdbCluster);
Michael Kenzel
  • 15,508
  • 2
  • 30
  • 39