|
Search Tech-Forums - link takes you to our Forum's search page. Note: The following is only a text archive! To view the actual forum discussion, please visit our website at http://www.tech-forums.net Pages:1 problem with my C++ code(Click here to view the original thread with full colors/images)Posted by: weewun im a pretty amature programmer, so forgive me sounding silly. if i create a class, lets call it 'data', then locally declare a member of this, E.G. 'data person;'. i am wondering how i would pass the 'person' into another function dynamically, so if anyting changes it in this new function, it is changed in the origional function. i have tried experimenting with pointers and referances, but im not sure how to do it. if any1 can help, please do so! cheers Posted by: forrestcupp Here is a simple console program that demonstrates what you are wanting. I made a class called "data" with an integer called person. The constructor for this class sets person = 11. Then I made another class called "other" just to show that it is definitely out of scope of the data class. This class has a function that takes a pointer as an argument. When I called this function from the main function, I sent the reference of the integer "person" from the data object that I created. A reference is the address of a variable. A pointer is a variable that holds a reference address. In this "other" function, the value is changed to 52, which affects the original variable. [CODE]#include "stdafx.h" #include <iostream> using namespace std; class data { public: int person; data() { person = 11; return; } }; class other { public: other(){}; void changeVariable(int *change) { *change = 52; return; } }; int _tmain(int argc, _TCHAR* argv[]) { data myData; other myOther; cout<<myData.person<<endl; myOther.changeVariable(&myData.person); cout<<myData.person<<endl; cin.get(); return 0; } [/CODE] Posted by: weewun sorry :( i dont think i said what i was meaning too... what im wanting to do is have a class 'data' then initialise, like 'int numbe;' called person, then pass through the entire class... e.g. .......................................................... class data { int itemone; int itemtwo; }; int anotherfunction(data *aperson)//pass the whole class through { aperson.itemone=10; return; } int main(){ data person; anotherfunction(&person); } ........................................................... i am coding on a PS2 development kit, so the debug is not ver helpful, but for the code above it would say: request for member `itemone' in `aperson', which is of non-aggregate type `data *' cheers for the help Posted by: weewun haha! i found out what iw as doing wrong! i gotta use (*aperson).itmeone=10; cheers for the help again! vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited. PPC Management vB Easy Archive Final - Created by Xenon |