CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Location
    Tampa, FL
    Posts
    114

    Right way to access another class's objects

    I keep getting this basic thing wrong and i need some help.

    let’s say a class ‘A’ includes an Array of class “B” objects, and also includes some management class called, “M”. Assume each array element of class B has its own setters and getters for some internal boolean flags.

    Now let’s say M ( included in Class “A”) is running its own thread, and while that thread is executing it needs to be able to read and write to some of the class B array element setters and getters back in class A.

    What would be the correct way of accessing those class B element setters/getters from class M, to ensure I’m really addressing the original objects and not some copies? I thought I could just pass "M" a 'this' pointer of class type 'A', save a copy, and then use that object to access the 'B' array. But often I'll do that and it seems that as far as 'A; is concerned, it's 'B' array objects haven't changed! So I must be accessing a copy of the object when I pass my 'this' pointer, and failing to actually access A's original 'B' objects.

    I've asked this to other more advanced colleagues, and have been advised to “just use statics”, and maybe an Arraylist (which must be static). Then, within 'M', I could just declare an instance of class ‘A’ and call the setters/getters, since all statics are common to all class instances. But that seems like a cop-out to me, and I’m still not learning the RIGHT way to do it.

    Thanks for any help!

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Right way to access another class's objects

    Quote Originally Posted by Randy C View Post
    What would be the correct way of accessing those class B element setters/getters from class M, to ensure I’m really addressing the original objects and not some copies? I thought I could just pass "M" a 'this' pointer of class type 'A', save a copy, and then use that object to access the 'B' array. But often I'll do that and it seems that as far as 'A; is concerned, it's 'B' array objects haven't changed! So I must be accessing a copy of the object when I pass my 'this' pointer, and failing to actually access A's original 'B' objects.
    This as very coupled design but anyway.

    I don't see how a "copy" should come about unless you create one. Do you? If an A object is updated from an M object then this change should take effect in the A object. Why not?

    But one possible problem is that if there are several A objects and they all share the same M object then just one of the A objects will be updated (the last one to pass its this pointer to the M object). All other A objects will remain unchanged.

    Why don't you insert trace statements in the getters/setter and try to follow what actually takes place.
    Last edited by nuzzle; September 12th, 2011 at 04:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured