Thread: still with pointers

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    still with pointers

    Hi all,

    Again for learning purposes, I'm creating a vector of pointers to string and then access the underlying strings. After a little trial and error, I produced the following (showing where I read the vector)...

    Code:
    /* ... */
        for(vector<string*>::const_iterator ster = svec.begin(); ster != svec.end(); ++ster)
            cout << **ster << "\t" << (*ster)->size() << endl;
    /* ... */
    It works and I understand why. But I do have a couple of questions:

    1.
    My main problem was achieving (*ster)->size, despite the fact I'm already somewhat familiar with the -> operator from past exercises. Everything else went like a breeze. But although I do understand why it works (dereference the iterator ster, then dereference the resulting pointer to string, and then apply the dot operator to resulting string class), I would like to understand this better. I'm trying to expand that statement to just the * and . operators.

    *(*(ster)).size() is not doing it. I cannot fathom why.

    2.
    I'm wondering about size_type. Should these follow strictly the same type as the underlying vector only for aesthetic and logical reasons, or are there any cases in which not doing it may produce bad results?

    I could for instance, not have used iterators on the code above and use instead subscripts. The funny thing is that the control flag could have been either vector<string*>::size_type i = 0 or vector<int>::size_type i = 0 and I would still have a perfectly valid, error and warning-free code.

    I'm not thinking being lazy about it. But would like to know if there can be any side-effects for not following what seems to be the wisest choice (declare size_type as the same type of the underlying vector)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The reason it's not working is because of operator precedence. This should work: (**ster).size()

    I'm wondering about size_type. Should these follow strictly the same type as the underlying vector only for aesthetic and logical reasons, or are there any cases in which not doing it may produce bad results?

    I could for instance, not have used iterators on the code above and use instead subscripts. The funny thing is that the control flag could have been either vector<string*>::size_type i = 0 or vector<int>::size_type i = 0 and I would still have a perfectly valid, error and warning-free code.
    You should follow the underlying type just to keep people from getting confused and raising an eyebrow when looking at your code. Either way, the size_type is just a typedef'd integral type (probably unsigned int or long) so both vector<string*>::size_type and vector<int>::size_type both reference the same type of object (whatever it may be).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by hk_mp5kpdw
    The reason it's not working is because of operator precedence. This should work: (**ster).size()
    Doh! Of course!
    Thanks

    And thank you too for the explanation on size_type
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM