-
April 12th, 2006, 08:03 AM
#1
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.
-
April 12th, 2006, 08:39 AM
#2
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
-
April 12th, 2006, 09:03 AM
#3
Re: Passing object references to classes
-
April 12th, 2006, 09:25 AM
#4
Re: Passing object references to classes
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 12th, 2006, 02:46 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|