const and const_cast issues -please help
I could really use help, please! I'm stuck and frustrated.
I've got:
public const void *myFilePtr
and I need to cast it as a:
int *FilePointer
I've tried using:
int *Filepointer = const_cast<int *>(myFilePointer);
but the class I'm trying to do the cast in is a const class too and I keep getting the error:
passing `const namespace' as `this' argument of `int namespace::myBinarySort(int *filePointer, const string& searchString, int low, int high)' discards qualifiers"
Which I know means I'm still using a const when I shouldn't.
Can someone tell me how to cast this correctly so this const stuff goes away? It's not an option to change what things are marked const sadly.
More Details in case that's not enough:
in program.h
public const void *myFile;
and I have to implement the following class in program.cpp:
bool namespace::getInfo(const string& name, vector<foo>& foo) const
from this class I need to call a binary sort function I wrote as follows:
int namespace::myBinarySort(int *filePointer, const string& searchString, int low, int high)
which I need to return an int pointer to the memory address where searchString is found
And I make the call like this:
int myData = myBinarySearch(mySendingFile, myString, low, high);
So the part I'm having trouble with is correctly casting mySendingFile as int *mySendingFile;
Thanks very much if anyone can help me get unstuck!
Re: const and const_cast issues -please help
Please use [code][/code] tags when posting code.
Please provide a minimal, but complete example of your problem. The (pseudo)code snippets you have posted are unrelated.
Re: const and const_cast issues -please help
Sorry, I've never posted for help before I'm new to the forums. I thought the information I provided was relevant. I'm sorry for wasting your time. I obviously just do not know what I'm doing at all. So sorry.
Re: const and const_cast issues -please help
This is done in 2 steps :
a) cast away constness
b) convert void* to int*
Code:
#include <iostream>
using std :: cout;
using std :: endl;
int main()
{
const void *myFilePtr = new int; //Source Pointer
void *myFilePtrNonConst; //Intermediate Pointer
int *FilePointer; //Destination Pointer
//Casting is done in 2 steps
myFilePtrNonConst = const_cast<void *>(myFilePtr); //Cast away constness
FilePointer = static_cast<int*>(myFilePtrNonConst); //Convert void* to int*
system("clear");
cout << "myFilePtr = " << myFilePtr << endl
<< "myFilePtrNonConst = " << myFilePtrNonConst << endl
<< "FilePointer = " << FilePointer << "\t*FilePointer = " << *FilePointer << endl << endl;
*FilePointer = 20;
cout << "myFilePtr = " << myFilePtr << endl
<< "myFilePtrNonConst = " << myFilePtrNonConst << endl
<< "FilePointer = " << FilePointer << "\t*FilePointer = " << *FilePointer << endl << endl;
return(0);
}
I found this article on casting helpful, see if helps:
http://www.acm.org/crossroads/xrds3-1/ovp3-1.html
Re: const and const_cast issues -please help
That second case may need to be a reinterpret_cast. I'm not positive though.
Re: const and const_cast issues -please help
Quote:
Originally Posted by Lindley
That second case may need to be a reinterpret_cast. I'm not positive though.
static_cast is appropriate for a conversion from void* to int*.