soccermiles wrote: |
---|
Also...what is class delimiting?? |
It marks the limits of the class. Class declarations in C++ need to end with a semi-colon:
1 2 3
|
class Q
{
}; // note the ';' to end the class
|
soccermiles wrote: |
---|
When should I pass references and when should I pass pointers to functions? |
Pass pointers when you want to have fun... ;o)
But normally pointers should really be avoided if possible. However the task will usually lend itself to either one or the other. A rule of thumb is to use a reference unless you have to use a pointer. But sometimes pointers make more sense.
When passing objects as parameters to functions, the preferred option is to pass a const reference like this:
|
void func(const std::string& name);
|
This ensures that the variable passed in to the function is safe from modification by the function. Also its more efficient than passing a copy:
|
void func(std::string name);
|
And it is less dangerous than passing pointers.
If you want the function to modify the variable that you pass in, then you can use a non-const reference:
|
void make_uppercase(std::string& name);
|
You might end up working with pointers. If you need to store non-copyable objects in a container, for instance, you would need to store pointers to them. In that case I would still be tempted to write my functions to accept references and simply dereference the object pointer when I called the function:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
void func(const MyType& m)
{
// .. do stuff with m
}
int main()
{
MyType* mp = new MyType;
func(*mp);
delete mp; // remember to clean up ...
}
|
That keeps my interfaces as pointer free as possible.