CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    79

    Passing object references to classes

    Is this sort of thing safe to do (passing by reference)?

    Code:
    View::OnIntialUpdate()
    {
          pPAInterface = new CPAInterface;
    
          pPan = new CPan(*pPAInterface);
    
          pDec = new CDec(*pPan);
    }
    The reason I ask is I want the data stored in the CPAInterface class to be accessable and modifyable by the CPan class, and the data held in Cpan to then be accessable by CDec.

    Thanks for any advice.

  2. #2
    Join Date
    Feb 2006
    Location
    London
    Posts
    238

    Re: Passing object references to classes

    If you know what your are doing, if you sure that your design is right, if you considered the lifetime of all created objects, then it is Ok

  3. #3
    Join Date
    Apr 2005
    Posts
    79

    Re: Passing object references to classes

    Thanks

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Passing object references to classes

    Quote Originally Posted by jdm2104
    Is this sort of thing safe to do (passing by reference)?

    Code:
    View::OnIntialUpdate()
    {
          pPAInterface = new CPAInterface;
    
          pPan = new CPan(*pPAInterface);
    
          pDec = new CDec(*pPan);
    }
    The reason I ask is I want the data stored in the CPAInterface class to be accessable and modifyable by the CPan class, and the data held in Cpan to then be accessable by CDec.

    Thanks for any advice.
    Why not have members of the required types in the respective classes? CPan would have a member CPAInterface and CDec would have a member CPan. Now when you construct CDec you simply do:
    Code:
    CPan obj_CPan(CPAInterface());
    CDec obj_CDec(obj_CPan);
    Otherwise, if this doesn't suffice what you want - you could have the member functions operating on the objects take the respective arguments by reference. That would be a better way to go about achieving this and would not necessitate the need for dynamic allocations. Hope this helps. Regards.

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Passing object references to classes

    As exterminator pointed, you probaby don't need dynamic allocation.
    And, as a proof, I can show that your code has a memory leak:
    You don't free the object pointed-to by pPAInterface, and don't save this pointer anywhere.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

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