G
Gonçalo Rodrigues
Hi all,
I have a class, call it Object of heap-allocated objects. They are
managed via a smart pointer class Ref (actually a template class but
that does not matter for the problem). Object instances are created
via a factory method that returns a Ref and all constructors are
protected.
My "problem" is the following. The Ref class is basically a pointer,
e.g. something like
class Ref {
public:
//... some methods.
private:
Object* ptr;
}
and it supports all the usual operations for smart pointers like *,
->, assignment -- is particular ptr is *not* const.
The constructor is, basically
(A)
Ref(Object* ptr) : ptr(ptr) {
//Do some stuff if this->ptr != 0.
}
Now, looking at the signature of the constructor, my first temptation
was to write
(B)
Ref(const Object* ptr) : ptr(ptr) {
//Do some stuff if this->ptr != 0.
}
Because in the "do some stuff" part I do *not* change in any way the
pointed to Object. But, of course, the compiler complains because I'm
assigning a const to a non const. So I was tempted to do
(C)
Ref(const Object* ptr) : ptr(const_cast<Object*>(ptr)) {
//Do some stuff if this->ptr != 0.
}
but that const_cast sure does leave me nervous.
The reason I want to make the signature of the Ref constructor as in
(B) is because a lot of methods create Ref's to their arguments but
otherwise do *not* change them, so, it seems to me, I should declare
them const but because of (A) I cannot. Any advice on this? Is (C) the
best solution? Am I failing to understand the meaning of const?
Best regards,
G. Rodrigues
I have a class, call it Object of heap-allocated objects. They are
managed via a smart pointer class Ref (actually a template class but
that does not matter for the problem). Object instances are created
via a factory method that returns a Ref and all constructors are
protected.
My "problem" is the following. The Ref class is basically a pointer,
e.g. something like
class Ref {
public:
//... some methods.
private:
Object* ptr;
}
and it supports all the usual operations for smart pointers like *,
->, assignment -- is particular ptr is *not* const.
The constructor is, basically
(A)
Ref(Object* ptr) : ptr(ptr) {
//Do some stuff if this->ptr != 0.
}
Now, looking at the signature of the constructor, my first temptation
was to write
(B)
Ref(const Object* ptr) : ptr(ptr) {
//Do some stuff if this->ptr != 0.
}
Because in the "do some stuff" part I do *not* change in any way the
pointed to Object. But, of course, the compiler complains because I'm
assigning a const to a non const. So I was tempted to do
(C)
Ref(const Object* ptr) : ptr(const_cast<Object*>(ptr)) {
//Do some stuff if this->ptr != 0.
}
but that const_cast sure does leave me nervous.
The reason I want to make the signature of the Ref constructor as in
(B) is because a lot of methods create Ref's to their arguments but
otherwise do *not* change them, so, it seems to me, I should declare
them const but because of (A) I cannot. Any advice on this? Is (C) the
best solution? Am I failing to understand the meaning of const?
Best regards,
G. Rodrigues