Pelle said:
Hi all,
I have a few newbie questions:
In function declaration what does a 'const' mean
inside the parameter list ? That it won't modify the value?
void MemberFunction(const int x);
Yes, but in this example it's meaningless -- 'const' on a parameter is
meaningful wrt a pointer or reference:
void MemberFunction(const int &x);
void MemberFunction(const int *x);
in these cases it isn't really the parameter itself that the function
promises it won't modify, but the object that points at/refers to.
Since the parameter (unless it's a reference) is always a copy of some
value, changing it doesn't really mean much.
What does 'const' before a function dec. mean?
const void MemberFunction(int x);
It means that what's being returned is a const (whatever). Again, this
example has a minor problem -- since a function with a void return type
doesn't return anything, you can can't declare that nothing that's
being returned as const.
Something like:
const int MemberFunction(int x);
does make some sense, though a const on the return is more common when
you're returning something like a full-fledged object.
No, not even similar. The const affects the type of object being
returned. In the same place, static would affect the type of the
function itself -- assuming (as the name implies) that this was a
member function, it would make it a static member function. A static
member function is almost comppletely different from a normal member
function because it's not associated with a particular instance of the
object.
My pointer skills aren't perfect, so what is
the differece between these functions?
void MyFunction(int& x);
void MyFunction(int* x);
The first receives a parameter by reference, while the second recieves
a pointer. The two often accomplish similar things, but a reference
always refers to the same object for its entire lifetime, while a
pointer can be changed so it points at different objects at different
times (or to nothing at all, if you so desire).
Why can't ctors and dtors be "totally virtual"?
class MyClass {
virtual MyClass() = 0;
virtual ~MyClass() = 0;
};
That's "pure virtual", in case you care. In any case, a dtor _can_ be
pure virtual if you want. A ctor can't be virtual -- or is always
virtual, depending a bit on your viewpoint, but in any case, you can't
specify it as virtual at all, and without that, pure virtual is
completely out of the picture.
How do I specify that one class should inherit
both public AND protected members from a base class?
Using 'friend'?
Anytime you do inheritance, you derive both public and protected
members from the base class.
And what's so very good about iterators compared
to an ordinary for-statement?
As in:
for(vector<int>::iterator etc ... )
vs.
for(int i = 0; i < myvector.size() etc ...)
In this particular case, the iterator provides little real advantage --
the primary reason for using iterators is to pass them to algorithms.
The reason algorithms use them is to abtract iteration, so the
algorithm can use the same code to work with different kinds of
containers. Getting from one node in a linked list to the next is a lot
different from getting from one element in an array to the next, but
iterators allow you to write your code the same way to do either (or to
work with various other containers as well). At least in theory, this
makes it easier to substitute one container for another, though there
are some limitations on this -- the iterator gives a common syntax, but
doesn't change the fundamentals, so an iterator won't (for example)
give you random access to data that's arranged to only allow serial
access.