Hi,
sry I have some Java experience but I am a rookie in C++. Looking at the reference of this website for the Vector, I was surprised that the method vector::at has two definitions:
const_reference at ( size_type n ) const;
reference at ( size_type n );
Does that mean that is possible to define a method twice as a property in a get and set manner? Moreover the keyword "reference" used here is really a C++ type or is just a friendly shorthand for the template type?
My second question, I am uncertain how overload of functions and methods works. I found out that C++ does not allow to define two functions as :
string& toString(int n);
string& toString(long long n);
so what are the rules for the methods (or functions) overloading?
If your compiler supports longlong you can do the overload. *
As long as the prototype changes (and it is not ambiguous), it should work. The prototype does not include the return type,
1 2 3 4 5
typedef T& reference; //This is the definition in allocator
typedefconst T& const_reference;
const_reference at ( size_type n ) const; //this one is called with constant objects.
reference at ( size_type n );
I don't see those methods as getters/setters. The responsibillity of the container is to provide access to the contained elements.
It should not care what they are.
The idea of a constant object does not realy exist in Java. Think of it as an immutable version of an object. Any methods declared as const (after the signature, i.e. void Class::f(Type x) const) can be used on a const version of that object, but can't change that objects members.
1 2 3 4 5 6 7 8 9
class Class {
private:
int z;
public:
Class(int n) : z(n) {}
void setZ(int n) { z = n; }
int getZ() const { return z; }
};
thanks for your clarifications. Just a last note, do these rules for overloading work only for instance methods or also for global functions? I failed to compile the aforementioned code under GCC 4.5 /Mingw :
Typically, however, it actually makes no difference -- a machine integer is a machine integer, right? Use some #macro magic and build a single function that takes the largest integer type you have access to. This obviates (nearly) all type-promotion issues...