LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2005, 05:23 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
changing objects parameters via getters inside functions in c++?


Hi,

I have an object A and pass it some function that calls its setters to reset some data members.

e.g.

void A(B b){ b.SetWhatever(2)}

then after calling A b indeed has the parameters reset. The problem comes when I pass b to a function C that calls A. Now inside C, b again has had its parameters changed. But externally to C b has not had its parameters changed,

e.g

void C(B b){ A(b) ;}

why does b have different values for its data members inside C, than outside C? After all in both places it is residing at the same memory address surely?

Thanks,

Mark
 
Old 03-08-2005, 05:30 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Why don't *you* check if they have same address?

Simple rule: If you want changes made by a function to one of its argument to be visible to the caller of that function, that argument should be passed by reference or by pointer. If you are using objects that are big and/or costly to construct you always pass those by reference or pointer, only make the parameter const if you don't intend to change the state of the object in the function.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing Kernel Parameters aslamg Linux - Newbie 13 01-20-2006 02:57 AM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
changing keyboard keys functions grizzly Slackware 2 01-10-2004 09:05 PM
cant get main to call functions, unsure of parameters. c++ calypso_craig Programming 5 10-28-2003 05:47 AM
Changing Kernel Parameters aslamg Linux - Newbie 2 09-03-2002 05:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration