using objects and pointers

This is a discussion on using objects and pointers within the C++ Programming forums, part of the General Programming Boards category; Hello, I am re-engineering someone elses code and have come across some code I don't really understand. To keep it ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    using objects and pointers

    Hello,

    I am re-engineering someone elses code and have come across some code I don't really understand.

    To keep it simple I have just displayed the code snippets.

    I have a class called Board
    Code:
    class Board
    {
    	private:
    	Board *_boards[10];
    
    	public:
    	Board* const* boards() const; 
    .
    .
    .
    };
    
    Board* const* Board::boards() const
    {
    	return _boards;
    }
    Now I have another a cpp file that using this class.
    I have this code which I find hard to understand.
    Code:
    Board *board = static_cast<Board*>(boards()[i]);
    I don't know why there is a [i] at the end of the boards() function?

    Any advice would be most gratefull,

    Thanks,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Quote Originally Posted by steve1_rm
    I don't know why there is a [i] at the end of the boards() function?
    It is another way of writing:
    Code:
    Board *board = static_cast<Board*>(*(boards() + i));
    That said, the static_cast looks unnecessary since you are copying a pointer that is const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for the response.

    So that this is really doing is:
    Code:
    (*boards()  + i))
    dereferencing and returning the board pointer object?

    However, just one more question.

    I have a member of that class. Called _boardID which is public. However, should be private for for testing purposes I have it as public.

    But I can't seem to access using the index:
    Code:
    board[0]._boardID = 1001;
    However, I can do this:
    Code:
    board->_boardID = 1001;
    I thought I should be able to access using both methods. However, the first one is needed as I need direct access to some elements.

    Any thoughts about this?

    Many thanks,

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would also be a bit suspicious as to the correctness in a design where a member variable is a pointer to an array of the same class - doesn't seem quite right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for your input.

    Can you please explain your comment. I not sure I fully understand.

    Many thanks,

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, it is (most of the time) not quite right to have a pointer to the own object type. Exceptions would be for linked lists and trees of various kinds. But I don't thihk that is what you are doing?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2003, 11:23 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2001, 11:57 PM

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