Thread: restrictions on pointers using const

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    restrictions on pointers using const

    Hey guys,
    SO...I was feeling confident in my understanding of the various uses of const with pointer declarations to restrict how they function...I was doing fine until I came across a bit of code that I modified thinking that a const pointer to const would do fine...tell me what I'm missing here...here's the full code minus the preprocessor directives and I'm using the std namespace:

    Code:
    //returns a pointer to a string element
    string * ptrToElement(const vector<string> * const pVec, int element);
    
    int main()
    {
        vector<string> inventory;
        inventory.push_back("sword");
        inventory.push_back("shield");
    
        cout << "Sending the object pointed to by a return pointer:\n";
        cout << *(ptrToElement(&inventory, 1)) << endl;
        return 0;
    }
    
    string * ptrToElement(const vector<string> * const pVec, int i)
    {
        return &((*pVec)[i]);
    }
    Ok...so...the const pointer to const was my addition and the logic behind it is I just don't see any change to the value of the element in the function...so why do I get a compile error? Any insight would be very very helpful...even looking over it now, all I'm doing is accessing a value...I'm not altering it...anyways, any help would be great. Thanks, Chap

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'm not sure if I understand what you're asking, but I'll give it a shot. You don't need the second const. The first const is enough; with the first one, you're already declaring a const vector<string>*. Maybe you mean something like this:
    Code:
    const string * ptrToElement(const vector<string> * pVec, int i)
    {
        return &((*pVec)[i]);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    ok...I see your point...the const pVec is not neccessary, however....would it be a good idea to leave it in just incase I decide to expand the function but with the intention of never having pVec point to anything else? I had another question but I can just figure it out by working with the code...Oh one more question (sorry to be a pain...just trying to understand as much as possible) why is it that I get a compile error if I do this:
    Code:
    string * ptrToElement(const vector<string> * pVec, int i)
    but it compiles fine when I have this:
    Code:
    const string * ptrToElement(const vector<string> * pVec, int i)
    Thanks for the help by the way...you're awesome, Chap
    Last edited by Chaplin27; 01-25-2005 at 10:29 AM. Reason: I'll figure it out on my own

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 04:22 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM