Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unclear about object referencing between functions...

Status
Not open for further replies.

Daieseo

Technical User
Mar 19, 2006
3
GB
Hi, I have just been getting into using classes etc, but what im coming unstuck with is how a particular object that has been created maybe accessed globally by any function in the program - IE one function that creates the object and then another that changes some of its attributes at a later time.

Perhaps im just going about this all wrong - but any advise on how this is done and the best way to do it would be great.

Thanks
Dan
 
The best way is to pass a reference of that object to the function. Ex:
Code:
ObjectA CreateObjectA( /* some parameters */ );
void ModifyObject( ObjectA& );

int main()
{
...
   ObjectA myObj = CreateObjectA( /* some parameters */ );
   ModifyObject( myObj );
...
}
Since ModifyObject() takes a reference to an ObjectA, it can modify myObj instead of modifying a local copy of myObj.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top