Get & Set properties in C++??

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?

Thanks for any consideration
With regards
- Atari

But you can do..
1
2
string& toString(int n);
string& toString(long num1, long num2, int n);
If your compiler supports long long 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
typedef const 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.

*¿is it part of the standard yet?
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 :
1
2
string& toString(int n);
string& toString(long long n);


with regards
- Atari
That is because of obnoxious type-promotion rules that you are having problems with the overloads.

My own observations about type-promotion issues can be found here:
http://www.cplusplus.com/forum/lounge/32041/3/#msg179232

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...

Alas.
I think declaring them as explicit might solve the problem, but I'm not sure.
1
2
explicit string& toString(int n);
explicit string& toString(long long n);
Yes, but then you have to declare a toString() function for every integer type.

A thought occurs, though: use template magic to create the default function, and a specialization to handle the odd-ball version(s).

:-)
Topic archived. No new replies allowed.