tags:

views:

199

answers:

2

I am not sure how to have a function that receives a class object as a parameter. Any help? Here is an example below.

#include<iostream>

void function(class object); //prototype

void function(class tempObject)
{
   //do something with object
   //use or change member variables
}

Basically I am just confused on how to create a function that will receive a class object as its parameters, and then to use those parameters inside the function such as tempObject.variable.

Sorry if this is kind of confusing, I am relatively new to C++.

+6  A: 

At its simplest:

#include <iostream>
using namespace std;

class A {
   public:
     A( int x ) : n( x ){}
     void print() { cout << n << endl; }
   private:
     int n;
};

void func( A p ) {
   p.print();
}

int main () {
   A a;
   func ( a );
}

Of course, you should probably be using references to pass the object, but I suspect you haven't got to them yet.

anon
Pass by ref is a necessary quirk of C++ that *needs* to be introduced up front, even if the user doesn’t understand it yet. In particular, the OP wanted to *modify* the class members.
Konrad Rudolph
anon
+1  A: 

class is a keyword that is used only* to introduce class definitions. When you declare new class instances either as local objects or as function parameters you use only the name of the class (which must be in scope) and not the keyword class itself.

e.g.

class ANewType
{
    // ... details
};

This defines a new type called ANewType which is a class type.

You can then use this in function declarations:

void function(ANewType object);

You can then pass objects of type ANewType into the function. The object will be copied into the function parameter so, much like basic types, any attempt to modify the parameter will modify only the parameter in the function and won't affect the object that was originally passed in.

If you want to modify the object outside the function as indicated by the comments in your function body you would need to take the object by reference (or pointer). E.g.

void function(ANewType& object); // object passed by reference

This syntax means that any use of object in the function body refers to the actual object which was passed into the function and not a copy. All modifications will modify this object and be visible once the function has completed.

[* The class keyword is also used in template definitions, but that's a different subject.]

Charles Bailey
It's not like the class keyword is illegal as he uses it, though. The following is a perfectly valid TU: `void f(class foo);`. If "foo" is not yet known, it will be introduced there, so it's a forward declaration at an uncommon place though.
Johannes Schaub - litb
@litb: 100% correct, but probably more confusing than helpful for a beginner. I've had some experience with C++ but I'd be doing a double or triple take if I came across `void f(class foo);` in the wild.
Charles Bailey
Perfect, thanks for the extended explanation. Much appreciated!
James