C
ciccio
Hi all,
I'm lately a bit puzzled about something and it boils down to the
usage of the keyword const.
I'll sketch the problem and hope to find enlightenment here in this
group.
I would like to create some kind of object and proxies of that
object. A simple example would be a vector and the proxy is then a
subvector.
Assume I have the following array class that contains a simple array
definition
class array {
public:
typedef unsigned int size_type;
array() : data_(0), size_(0) {}
array(size_type size) { /* definition here */ }
array(const array &c) { /* copy constructor */ }
// all requested functions and operators here
private:
double * data_;
size_type size_;
};
This array class would then contain the data of my vector.
The private data members of the vector class are then an array
and an array reference. The reference is used for all data access and
thus in all functions. The vector constructors set the reference.
In case of a proxy, the reference is set to data of the original.
class vector {
public:
typedef array::size_type size_type;
vector() : _data_(), data_(_data_) { /* def here */ }
vector(size_type size) : _data_(size), data_(_data_) {
/* def here */
}
vector(const vector &c) : _data_(c.data_), data_(_data_) {
/* def here */
}
// proxy
vector(const vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
// all requested functions and operators here
private:
array _data_;
array &data_;
// bunch of variables you want here (size, begin, end, stride)
};
The question I have no concerns the proxy constructor, i.e
vector(const vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
The constructor takes a const vector reference p, and creates a new
vector that can alter the data members of p afterwards. This could be
done by means of other functions. (Ex.
vector v(20); // vector v of size 20
vector p(v,10) // vector p has same elements of v but only address
// first 10
p(1) = 1.0;
How correct is this with respect to the idea of const?
I could define the constructor as
vector(vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
but this would make it impossible to use
vector(vector(p,i),j)
because of the dangling temporary that one of the references has.
So, how far should the const keyword reach? If an object is defined as
const, should the proxies or all derived objects also be consts?
I don't know, I am a bit puzzled at the moment.
Regards
I'm lately a bit puzzled about something and it boils down to the
usage of the keyword const.
I'll sketch the problem and hope to find enlightenment here in this
group.
I would like to create some kind of object and proxies of that
object. A simple example would be a vector and the proxy is then a
subvector.
Assume I have the following array class that contains a simple array
definition
class array {
public:
typedef unsigned int size_type;
array() : data_(0), size_(0) {}
array(size_type size) { /* definition here */ }
array(const array &c) { /* copy constructor */ }
// all requested functions and operators here
private:
double * data_;
size_type size_;
};
This array class would then contain the data of my vector.
The private data members of the vector class are then an array
and an array reference. The reference is used for all data access and
thus in all functions. The vector constructors set the reference.
In case of a proxy, the reference is set to data of the original.
class vector {
public:
typedef array::size_type size_type;
vector() : _data_(), data_(_data_) { /* def here */ }
vector(size_type size) : _data_(size), data_(_data_) {
/* def here */
}
vector(const vector &c) : _data_(c.data_), data_(_data_) {
/* def here */
}
// proxy
vector(const vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
// all requested functions and operators here
private:
array _data_;
array &data_;
// bunch of variables you want here (size, begin, end, stride)
};
The question I have no concerns the proxy constructor, i.e
vector(const vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
The constructor takes a const vector reference p, and creates a new
vector that can alter the data members of p afterwards. This could be
done by means of other functions. (Ex.
vector v(20); // vector v of size 20
vector p(v,10) // vector p has same elements of v but only address
// first 10
p(1) = 1.0;
How correct is this with respect to the idea of const?
I could define the constructor as
vector(vector &p, int i) : _data_(), data_(p.data_) {
// stuff where i can be used for, ex. different length
}
but this would make it impossible to use
vector(vector(p,i),j)
because of the dangling temporary that one of the references has.
So, how far should the const keyword reach? If an object is defined as
const, should the proxies or all derived objects also be consts?
I don't know, I am a bit puzzled at the moment.
Regards