Thread: Class with pointer to same class

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

    Class with pointer to same class

    Hi,
    I have designed a class in C++ that has four pointers to its 'neighbours' in the world. Its neighbours are of the same class, so I have ended up with a class that has a reference to itself. Sorry for the poor explanation, the code will make it more obivous:

    Code:
    class TerrainPatch
    {
    public:
    	TerrainPatch();
    	~TerrainPatch();
    
    	TerrainPatch* 	mNorthPatch, mEastPatch, mSouthPatch, mWestPatch; // <-- Problem
    };
    I can see why the compiler is upset with it, but I am unsure how to get round the problem. I could hold the 'neighbour' information outside the class if necessary, but the above approach would be most ideal. So any suggestions on how to code this?

    Cheers for the help,
    Skusey

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your compiler doesn't like it because mEastPatch, mSouthPatch and mWestPatch are not pointers but rather actual instances of the class and you can't do that. You need to indicate that these are also pointers:

    Code:
    TerrainPatch *mNorthPatch, *mEastPatch, *mSouthPatch, *mWestPatch;
    "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
    Registered User taelmx's Avatar
    Join Date
    Oct 2006
    Location
    Japan
    Posts
    55
    You need to make the "neigbors" pointers... I think thats wht you should do. I'm a little new to C++, but I think that is right.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    ahh... oh dear looks like my understanding of C++ is actually going backwards...

    Could someone explain the difference between:
    Code:
    TerrainPatch* blah;
    and...
    Code:
    TerrainPatch *blah;
    I see the magical star move back and forth in example code all the time, and I had just accepted it both meant the same thing, a pointer.


    BTW, thank you for the help hk_mp5kpdw, your code works perfectly!

    Skusey

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Reading hk_mp5kpdw's post again I see you are emphasising only the last 3 in the list, so I assume TerrainPatch* blah, and TerrainPatch *blah, both represent a pointer, and are the same then? Is there a standard way this should be used? The * with the datatype, or with the name?

    Cheers again,
    Skusey

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Could someone explain the difference between:
    There is no difference, but you always need * in front of every pointer.
    People have this strange thought, that this:
    Code:
    int* hello,hello2,hello3;
    makes them all pointers and equals to this:
    Code:
    int *hello,*hello2,*hello3;
    But as there's no difference between int *hello and int* hello, then the first one doesn't actually make them all pointers.
    Last edited by maxorator; 11-08-2006 at 09:40 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    They are just the result of writing style. Although it can generate some debate trying to answer which is best.
    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.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    TerrainPatch *mNorthPatch, mEastPatch, mSouthPatch, mWestPatch;
    Is the same as
    Code:
    TerrainPatch *mNorthPatch;
    TerrainPatch mEastPatch;
    TerrainPatch mSouthPatch;
    TerrainPatch mWestPatch;
    Which is not what you wanted, however ...
    Code:
    TerrainPatch *mNorthPatch, *mEastPatch, *mSouthPatch, *mWestPatch;
    Is the same as
    Code:
    TerrainPatch *mNorthPatch;
    TerrainPatch *mEastPatch;
    TerrainPatch *mSouthPatch;
    TerrainPatch *mWestPatch;
    Which as mp5kpdw pointed out, is probably what you wanted.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    They are just the result of writing style. Although it can generate some debate trying to answer which is best.
    That debate would be as meaningful as debating whether you should bite a sandwich from the left or from the right corner.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is no difference... for defining multiple pointers on a single line of code it helps me to focus on each of the variables as being pointers (or not if that's the case) if the * is as close to the variable name as possible instead of being seperated by however much whitespace. You can do it like this if you want:

    Code:
    TerrainPatch*	mNorthPatch, * mEastPatch, * mSouthPatch, * mWestPatch;
    The spacing doesn't matter for this example as long as each variable has a * to go along with it. It is easy (in my mind) to do the kind of mistake you made if the * is closer to the type instead of the variable name. If I'm doing single variables statements on each line of code, I may do things both ways since it doesn't matter in those cases. It's just clearer to me to keep the * close to the variable name if I have several pointer variable declarations to make on a given line.
    "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

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Your sandwiches have corners?

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    >That debate would be as meaningful as debating whether you should bite the sandwich from the left or from the right corner.

    Well, I wouldn't go that far. There's some good points on both sides and usually the reason has nothing to do with the pointers themselves but on how one usually declares their variables; one per line or more than one per line.
    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.

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by maxorator
    That debate would be as meaningful as debating whether you should bite a sandwich from the left or from the right corner.
    Obviously it's the middle (bend the bread and bite the side at the fold to make a hole when the bread is unfolded).
    "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

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So, then the middle way is to do this:
    Code:
    char*big,*small; //or
    char * big, * small;
    Obviously it's the middle (bend the bread and bite the side at the fold to make a hole when the bread is unfolded).
    I usually make thick sandwiches so they're not easily bendable.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    10
    Thanks for all the help everyone, one more piece of the C++ puzzle clear in my head, eventhough its now polluted somewhat with sandwich annolgies .

    Cheers again,
    Skusey

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Initializing a pointer in a class.
    By gpr1me in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2006, 03:05 PM
  3. class passing a pointer to self to other class
    By daioyayubi in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2005, 09:25 AM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM