Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-19-2007, 08:27 AM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 138

weewun is on a distinguished road

Default problem with my C++ code

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
weewun is offline  
Old 02-19-2007, 06:03 PM   #2 (permalink)
forrestcupp's Avatar
 
Ultra Techie

Join Date: Feb 2007

Posts: 588

forrestcupp is on a distinguished road

Default Re: problem with my C++ code

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;
}

forrestcupp is offline  
Old 02-20-2007, 06:00 AM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 138

weewun is on a distinguished road

Default

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
weewun is offline  
Old 02-20-2007, 06:24 AM   #4 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 138

weewun is on a distinguished road

Default

haha! i found out what iw as doing wrong! i gotta use

(*aperson).itmeone=10;

cheers for the help again!
weewun is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On