Creating fat object.

Can any one who can tell me how to create a fat object and pass it to function by copy and reference technique?

Thanks,
Ankit

1 Like

I assume that with “fat” object you ment some large object. In fact, to present copy and reference call, you do not need large object, it works similar with smaller ones.

In following code, structure is copied when passed to copy() function and it’s passed by reference when reference() method was called. The consequence of reference call is, that if you change object inside the method it’s also changed out of the method (that’s why I’m printing the value of n, to show that).

Here is the code:

#include <cstdio>

struct Fat {
	// this is really big structure...
	int n;
};

void reference( Fat* f ) {
	f->n = 9876;
}

void copy( Fat f ) {
	f.n = 5678;
}

int main() {
	// create structure
	Fat f1;
	f1.n = 1234;
	
	copy( f1 ); // structure is copied, so it's not changed
	printf( "after copy call: %d\n", f1.n );

	reference( &f1 ); // reference passed, changed inside
	printf( "after reference call: %d\n", f1.n );

	return 0;
}

If something is not clear, just ask :wink:

//