Pointer to a class as parameters

This is a discussion on Pointer to a class as parameters within the C++ Programming forums, part of the General Programming Boards category; class A{} class B{ A obj1; // class B contains an object of the class A public: void GetAddressForA (A*&xxx); ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    Smile Pointer to a class as parameters

    class A{}

    class B{
    A obj1; // class B contains an object of the class A
    public:
    void GetAddressForA (A*&xxx); // ('a reference on a pointer to
    // objects of the class A' - Is a parameter like that possible?' -
    // IF YES - what is the correct syntax? 'Class*&xxx' looks too
    // weird IMO)
    }

    Definition of that mysterious function:
    void GetAddressForA(A* &xxx){
    xxx = &obj1;
    }

    Somewhere in the program we define this:

    A* yyy; // A variable designed to store an address of any object
    // of the class A
    B kkk; // Also one object of class B;

    Then we call:

    kkk.GetAddressForA (yyy);

    My question:

    Will yyy contain the address of kkk.obj1 after a call to the function?

    Thx for help

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yikes...code tags and better variable nameing convention please. Yes it looks weird. Yes its correct. You could have tested this on your own. Why not use double indirection (i.e. Class **xxx)?
    Last edited by master5001; 12-19-2004 at 08:02 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why not use double indirection (i.e. Class **xxx)?
    Because it clarifies the declaration (provided you understand pointers properly) at the cost of cluttering up every use. I'd say that once you get a feel for the syntax, the reference method is much cleaner, and until you get a feel for the syntax, use it anyway so that you don't have to deal directly with more than one level of pointer.
    My best code is written with the delete key.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by master5001
    Yikes...code tags and better variable nameing convention please. Yes it looks weird. Yes its correct. You could have tested this on your own. Why not use double indirection (i.e. Class **xxx)?
    Personally, I don't find A*& any more weird than I am sillyI am sillyI am sillyI am sillyI am silly.

    EDIT: OK, let me try again: The last word should be A * *.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Personally, I don't find A*& any more weird than I am sillyI am sillyI am sillyI am sillyI am silly.
    Yet another reason to use T **p instead of T** p. Silly C++ers are always making things difficult.
    My best code is written with the delete key.

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, 09:18 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM

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