|
-
Oct 12th, 2006, 02:09 AM
#1
Thread Starter
Hyperactive Member
passing structs by reference
I have a class that has some struct types defined publicy:
Code:
//.h file
struct foo
{
int a;
string b;
char c;
};
void test(int a, foo f1);
Now in the .cpp file, I want to to have a function take a parameter as a type of that struct;
Code:
foo f1;
void test(int a, f1);
what is the correct way to pass and access this by reference:
test(1, &f1)
...
test(....)
{
*ft.a = 1;
}
no!
I am confused as to how pointers, the * and & operators and everything works with objects.
Can someone point me to a good tutorial or give me a simple snippet please?
thx.
-
Oct 12th, 2006, 06:54 AM
#2
Re: passing structs by reference
Code:
foo f1;
void test(int a, f1);
This is not a correct function declaration, here you declare a variable of type foo, and a function with no type for the last parameter. Function declarations always look like this:
Code:
void test(int a, foo f1);
If you want to use a reference instead you should replace foo by foo& so you get:
Code:
void test(int a, foo& f1);
To use f1 inside that function you can act as if it is a normal vriable, the only difference is that thing you pass as the parameter is modified.
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
|