pass by reference

i.e i have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class test {
	int age;
	string name;

public:
	test () {
	}

	test (int i, string n) : age (i), name (n) {}
	int age_caller () {
		return age;
	}
};

void testing_function (test &t) {
	cout << t.age_caller() <<  endl;
}

int main () [
	test t [5], *pt;
	//blablabla...
	testing_function (t); //error in this calling...
}


i don't know what's wrong. in fact, i think, there's nothing wrong with the syntax... and how can i initialize pt with t's arrays?
testing_function expects (a reference to) an object of type test. t is not an object of type test.

You can set pt to point at the array t with

pt = t; which is a synonym for pt=&t[0] although you still couldn't just pass pt, as pt is a pointer to an object of type test, and testing_function doesn't accept pointers to objects of type test. You would have to dereference pt on the way in;


testing_function (*pt);

There's no reason not to just use the t[i] notation;

testing_function (t[0]);

for example.
Last edited on
t is an array of test, but testing_function() is looking for a reference to a single test, not an array. Try this:
 
testing_function(t[0]);


if you want pt to point to the t array, then you can just do this (I think):

 
pt = t;


look at line 19 also :

 
int main () [ //  <- should be '{' 


-- edit: doh! I'm slow
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

using namespace std;


 class test {
	int age;
	string name;

public:
	test (int i = 0 , string n = "") : age (i), name (n) {}
	
    int age_caller () {
		return age;
	}
};

void test_function(test &t) 
{
	cout << t.age_caller() <<  endl;
};

int main () 
{
    test t[5], *pt;
	
    pt = &t[0];

    test_function(t[0]); 

    return 0;
}
but how to pass the "whole ts" (i mean with the whole arrays)? so we can access it within the function. and to assign the pt with the whole t arrays... use new? how?
Last edited on
how to pass the "whole t" (i mean with the whole arrays)?


Like this;

test_function (t);

so we can access it within the function


Within the function, access like this:

t[0], t[1], t[2], t[3], t[4]

Your test_function will have to expect a pointer to a test, like this:

void test_function(test* t)



how to assign the pt with the whole t arrays


like this:

pt = t;


I think you've misunderstood what an array is, and what a pointer is. I suggest you go back and read up on them again.




Last edited on
i got it, but @moschops what i mean is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

class test {
	int age;
	string name;

public:
	test () {
	}

	test (int i, string n) : age (i), name (n) {}
	int age_caller () {
		return age;
	}
};

void testing_function (test &t) {
	cout << t.age_caller() <<  endl;
}

int main () {
	test t [5], *pt;
	pt = new test [5];
	return 0;
}


i didn't try to test it further more but i guess it will work, maybe using pt + 2->age; and so forth... (but as i remembered, i've tried this way too. my bad... -_-)
If you want to pass more than one object, you should use vectors.
1
2
3
4
5
6
7
8
9
void testing_function (vector<test>& v) {
  assert(!v.empty());
  cout << v[0].age_caller() << endl;
}

int main () {
  vector<test> vec(5);
  testing_function(vec);
}
Last edited on
Topic archived. No new replies allowed.