Thread: Class Pointers

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Exclamation Class Pointers

    Hi im having a little problem understanding how class pointers work. Ill explain as best i can what i dont understand...

    My problem:

    Im trying to do a simple graph like data structure, and am having a few problems understanding how to reference the pointers and use them, also if im using them correctly.

    The class A should be able to point to 6 neighbours of class A, each of these neighbours in turn have 6 neighbours and so on.

    I have a class A like so

    Code:
    class A
    {
        public:
            A(); 
           ~A();
    
           A *one, *two, *three, *four, *five, *six;
    };
    In A how do i reference the one two etc? Im trying something like below but am having problems.

    Code:
    #include <iostream>
    #include "A.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
          A a,b;
    
          // a's first neighbour points to b's four neighbour
          a.one =  b;
          b.four =  a;
       
          return 0;
    }
    How do i get the pointer from a.one to point to object b? Am i using the correct notation? Sorry im new to pointers...!

    Also have i defined the class A correctly for the problem im doing?

    Sorry if this is a little vague!!!

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    a and b are not pointers, they are objects. To assign their addresses to pointers you need to use the address-of operator:
    Code:
    a.one = &b;
    b.four = &a;
    >Also have i defined the class A correctly for the problem im doing?
    You'll have fun with six links per object, but I don't see a problem with the declaration you have.
    My best code is written with the delete key.

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Thumbs up

    ah i see, thanks.

    so object 'a' s one pointer refers object 'b' s four pointer?

    Code:
                    a.one = &b;
    	b.four = &a;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so object 'a' s one pointer refers object 'b' s four pointer?
    Object a's one pointer refers to object b and object b's four pointer refers to a:
    Code:
      ~   ~
    ~   b   ~
      ~   a
    ~   ~   ~
      ~   ~
    This is what you want if you want two adjacent vertices in a graph structure representing six sided cells where the cells are defined as
    Code:
      2
    1   3
      @
    6   4
      5
    At least, that's what I'm assuming your graph looks like from the information you've given.
    My best code is written with the delete key.

  5. #5
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    yes thats exactly the sort of lattice style structure !

    so am i going about this the correct way?

    i was thinking of a 2D array of cells to store these in, to help with working out the correct neighbour of each

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i was thinking of a 2D array of cells to store these in
    I was thinking more of an undirected linked graph than an array. It's less awkward.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >I was thinking more of an undirected linked graph than an array. It's less awkward.
    What the heck is an 'undirected linked graph'?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    hm, is that much more difficult to do? im new with C++ (as u may have guessed !)

    could you explain it a little more?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What the heck is an 'undirected linked graph'?
    More or less: http://www.nist.gov/dads/HTML/undirectgraf.html

    I specified 'linked' to avoid confusion with an array based implementation, I was thinking pointer based.

    >is that much more difficult to do?
    Well, graphs are a pain no matter what implementation you go with. I just find the linked implementations to be more flexible and easier to work with than array based implementations.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing pointers to a templated class in a vector
    By rt454 in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2009, 03:04 AM
  2. Class pointers
    By Raigne in forum C++ Programming
    Replies: 17
    Last Post: 01-03-2007, 04:51 PM
  3. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM