Intro to C++ Frequently Asked Questions (FAQ)


Q. Is there a news group to support C++?
Q. What is 'function overloading'?
Q.What is name mangling, and why is it used?
Q. Why would you override the operator<<?
Q. Why do functions accept or return reference (&) parameters?

Q. Is there a news group to support C/C++?

A. The primary newsgroup where you can post questions is comp.lang.c++. If you find the newsgroup helpful, you should also try to answer questions there, as it will also help you learn. Just make sure your answer isn't wrong, otherwise someone else will!

Q. What is 'function overloading'?

A. Function overloading is where C++ permits you to write 2 or more functions with the same name. C++ uses name-mangling to actually create unique names for you, behind the scenes. Each overloaded function must have a unique list of parameter types, and the same return type.

Q. What is name mangling, and why is it used?

A. Name Mangling is something that all C++ compilers do, that is normally invisible to you. This is how C++ compilers support overloaded functions. When C++ comes across either a function call, or a function definition (the body of the function), it name-mangles the function name for the compile process (which is why it's normally invisble to you).

Name mangling is where the compiler creates a new function name for each function you have written (that isn't in an 'extern "C"' section). While the exact method used by each compiler varies, the name that a compiler creates for a function is a combination of the function name, and it's parameter list. For example, image you write the following two over-loaded functions:

void Foo( int X, int Y);
void Foo( long X, long Y);

The compiler will create names like the following for the above functions:


The same process is performed when when a function call is encountered, so a call to Foo(1,2) (because it has integers 1 and 2) will be a call to the Foo@int_int function.

As stated, this is normally invisible to you. But, if you were to declare the two functions above with prototypes, but actually only wrote the first version, then an attempt to call the second version (something like Foo(10000000,20000000)), would result in a link-time error, and the mangled-name of the function not found would be mentioned. You'ld see a message similar the following from your compiler:

Link Error: external function 'Foo@long_long' not found.


Q. Why would you override operator<<?

A. You normally provide an operator<< function for a class so that it can easily be output using cout. For example:

class Person
Person( char *NewName, int NewAge )
Age = NewAge;
const char* GetName() const { return(Name); }
int GetAge() const { return( Age ); }
char Name[256];
int Age;

ostream& operator<<( ostream& o, const Person& Per )
o << Per.GetName() << " is " << Per.GetAge() << endl;

Now, you could do:
void main()
Person Teacher( "Mario", 33 );
cout << Teacher; // Would output "Mario is 33"

Note how the operator<< function performs the output of the object, but to the ostream object (not directly to cout). In main, when cout is used, the compiler will search first for an operator<< in the ostream class (what cout is) written for a Person class. Of course, it won't find one because Person is our own class type. Next, the compiler will search for an operator<< function that accepts two parameters: what's on the left of the '<<' (an ostream), and what's on the right of the '<<' (a Person). It then finds our operator<< function, and calls that.

Note that in this example, the 'o' parameter in the operator<< function, will really be cout, as passed by main. But, since the function is written for an ostream, it can also be re-directed to a file or fstream object.

Q. Why do functions accept or return reference (&) parameters?

A. You will often see functions returning or accepting reference data types. The main advantages to doing so are speed, and the ability to change a value passed to a function.

An item passed via reference, can be changed by the called function (you don't have to pass the address of the parameter like in C, though that still works). Also, in passing and returning a value, a copy is not made, which speeds up execution. In the case of a class, using a reference also stops the copy constructor from being called.