CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    2

    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!

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Aug 2009
    Posts
    2

    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: const and const_cast issues -please help

    That second case may need to be a reinterpret_cast. I'm not positive though.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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*.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured