Newbie needs some help here. Please help

Started by
9 comments, last by rip-off 14 years, 11 months ago
Quote:Original post by goldenknight
Not hasty, it's just that all this time i could have learn C++, but no.... i need to learn poetry, culture, history. All these thing i learn from school aren't very useful. The only thing that i find useful and fun is math and how to read and write.(true story).

These things are useful, in their own way.

Quote:
I'm in trouble learning operator overloading, i think it's from absolute C++. Can you guys give me an eagle eye of this topic .

Operator overloading is one of those weird things that looks really weird and ugly when you write it, but makes the code using it less weird and ugly (if you do it right).

You should be familiar with std::cout by now. Consider what this means:
std::cout << "Hello " << playerName << ", how are you?" << '\n';

Here, we have a variable (std::cout) on the far left, and then a bitwise left shift operator, then a string literal, another variable, another bitwise left shift operator, and so on. These can't be actual bitwise shifts though, because these only have meaning for integral types.

What has happened is that std::cout (which is of type std::ostream) has overloads of "operator<<", which allow you to write string literals, integers, character literals, std::string instances and all manner of things to its stream.

Consider the alternative, using method calls (a fictional "output" function):
std::cout.output("Hello ");std::cout.output(playerName);std::cout.output(", how are you?");std::cout.output('\n');

This is several times longer.

The bitshift operator was chosen to replace such an "output" function because:
1) it has no real meaning for streams anyway
2) it looks a little like you are "sending" the right operand to the stream.

Aside from stream operations and an important corner case I will mention in a bit, operator overloading is rare enough in C++. Certain types, such as mathematical ones (games feature 2D or 3D vectors and also matrices) can benefit from their use.

The most imporant operator with regards to operator overloading it operator=. This is because if you create a type (using "struct" or "class", the two are nearly interchangeable) then if you do not overload operator=() then the compiler will do it for you. It will do the simplest thing, it calls the operator=() of all the members of the type.

This can result in problems if your class is in charge of a resource. Consider a "file" class, like std::fstream. It holds some kind of operating system file handle inside it. Such handles are usually primitive types, integers or pointers. Your file class probably obtains the handle in the constructor, and releases it in its destructor (an important C++ idiom called RAII).

However, if the compiler generated operator=() was used, it would create a copy of the raw file handle. This is bad. Consider if the copy now falls out of scope, its handle is released. However, this means that as far as the operating system is concerned, your program is finished with the file. So if the original tries to read or write to the file it will crash or an error will be generated. The proper thing for such RAII classes is to overload operator=() and do a "deep" copy of the data, or to suppress the compiler generated version (this is beyond the scope of this reply).

Quote:
And if you would be kind please give me an eagle eye and hints to learn pointers. (very confusing this one, hard to remember this point to what. You know what i mean right?)

Pointers are used (and abused) in C++ for achieving a number of important language goals. They are used with arrays, they are used to pass things by reference, they are used to dynamically allocate memory and must be used to store polymorphic types and optional semantics.

You may not be familiar with some of the above terms. We use pointers with raw arrays because C++ does not allow us to directly pass arrays as arguments to functions. Nor does it allow us to allocate variable sized arrays.

Pass by reference means to pass data to a function without making a copy of that data. This can be used to prevent costly copy operations when they are unnecessary, and to allow a function to "write back" changes through its parameters.

Pointers are used with dynamically allocated memory because that the way C++ allows you to handle such memory. They are used for storing polymorphic types because the size of polymorphic types can (and usually will) vary, and C++ does not allow us to access variable sized data without using pointers or references (and references are often unsuitable for long term storage of data because they are not reseatable).

We use pointers for optional parameters. This means that the function tests the argument to see if it is NULL before using it.

Some of the features in C++ were introduced specifically to reduce the number of places where pointers are used. The C++ "reference" modifier is one such example. Pointers are a huge source of errors (hence the "abused" above) in programs, so idiomatic C++ tries to avoid using so called "raw" pointers except where absolutely necessary.

Replacements include using standard containers instead of raw arrays, C++ references for pass by reference, "smart" pointers (such as the boost libraries smart_ptr section) to manage dynamically allocated memory and polymorphic type storage, boost::optional<> for optional semantics. All of the above use pointers "under the hood", but in a controlled way that eliminates many of the opportunities for error.

The above is to give you an idea of why these things are used, they won't necessarily help you learn them.

This topic is closed to new replies.

Advertisement