1

I haven't used void* and const_correctness before so I am not understanding what I am doing wrong in the below code. All I want is to cast a void* returned by a member function of a const object to int*. Please suggest better approaches. Thank you.

I get the following error

passing 'const MyClass' as 'this' argument of 'void* MyClass::getArr()' discards qualifiers

So here's the actual program that I had problem with

 class MyClassImpl{
    CvMat* arr;
    public:
        MyClassImpl(){arr = new CvMat[10];}
        CvMat *getArr(){return arr;}
};
class MyClass{
    MyClassImpl *d;
    public:
        const void *getArr()const{ return (void*)d->getArr(); }

};
void print(const MyClass& obj){
    const int* ptr = static_cast<const int *>(obj.getArr());
}
int main(){
    MyClass obj1;
    print(obj1);
}

Only the methods such as 'print()' in this case know the datatype returned by 'getData'. I can't use templates because the user doesn't know how MyClass is implemented. Thank you. Feel free to suggest alternatives.

Chenna V
  • 10,185
  • 11
  • 77
  • 104
  • Here is how you cast a void* to an int*... (int*)vp; Done. That said, why are you mucking around with void* pointers like this in C++ anyway? Templates my friend, templates... – Ed S. Jan 19 '11 at 00:18
  • Thanks Ed. How can I apply templates in this case? – Chenna V Jan 19 '11 at 00:24
  • @Ed: yep, got it .. I am changing my code. I don't know why I didn't think about templates. thank you – Chenna V Jan 19 '11 at 00:31

1 Answers1

5

I think the problem is not in the cast from your array to a void * but in trying to call obj.getArr() when obj is marked const and MyClass::getArr() is not a const member function. If you change your definition of that member function to

 const void *getArr() const { return static_cast<const void*>(arr); }

Then this error should resolve itself. You might want to do a const-overload as well:

 const void *getArr() const { return static_cast<const void*>(arr); }
       void *getArr()       { return static_cast<      void*>(arr); }
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • That worked, but I don't want to have const on the member function because there are other functions that change 'arr' through 'getArr' – Chenna V Jan 19 '11 at 00:20
  • @user521771- You can do this by making the signature `void* getArr() const;` However, you really should be thinking about whether this is a good idea - you're effectively lying about the constness of `MyClass` if you can mutate it even though it's const. – templatetypedef Jan 19 '11 at 00:23
  • thanks templatetypedef. ya there's definitely something wrong with the design. also do u have any suggestions on how I can avoid void* – Chenna V Jan 19 '11 at 00:29
  • @user521771- That really depends on the design, and without more information I don't know how much I can help. One question is why you're doing the cast to `void*` in the first place at all... why not just hand back an `int*`? – templatetypedef Jan 19 '11 at 00:32
  • I've added more to the code. Let me know what you think is wrong with the design – Chenna V Jan 19 '11 at 00:46