dereferencing pointers to pointers

This is a discussion on dereferencing pointers to pointers within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have a vector of pointers to objects called GamePiece I'm using an iterator to go through them ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    10

    dereferencing pointers to pointers

    Hi all,

    I have a vector of pointers to objects called GamePiece
    I'm using an iterator to go through them like this...

    Code:
    vector<GamePiece*> v = p1->getPieces();
      for (vector<GamePiece*>::const_iterator ci = v.begin(); ci != v.end(); ++ci){
        GamePiece *p = *ci;
        p->getType();
    }
    The code above compiles without error, but if I replace

    Code:
    GamePiece *p = *ci;
    p->getType();
    with

    Code:
     **ci.getType()
    I get errors about not being able to convert one type to another. What would I have to do to dereference ci in one line?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Seattle
    Posts
    30
    Quote Originally Posted by Potterd64
    Hi all,
    I get errors about not being able to convert one type to another. What would I have to do to dereference ci in one line?
    Not positive (just started working with iterators) but I think you want to do (**ci).getType();

    The only reason I'm not positive is because I'm not sure what you're doing with it but I'm positive that that's how you would dereference it correctly. Precedence comes in to play.
    Last edited by Peter5897; 07-11-2006 at 12:15 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Peter's right. Dereference has a lower precedence than membership. *a.b evaluates to *(a.b) when compiled.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    (**ci).getType() can also translate to (*ci)->getType();
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Dereferencing Pointers help
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:43 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21