U
Urs Thuermann
How should const be used? When I pass a pointer to some object to a
function which does not modify the object, I usually use const. But
when that pointer is later copied somewhere else, e.g. a list or
another function, I need to cast the const away, which I don't like.
class A;
class B {
const A *a;
public:
B(const A *a) : a(a) {}
void foo(std::list<A *> &l) {
l.push_back((A *)a); // the cast looks ugly
}
};
What exactly is the meaning of B(const A *a); ? Is that an assurance
to the compiler that the pointed-to object is not modified until B()
returns (enabling some optimizations), or does it mean that the object
is never modified through this pointer, not even later in some
completely other part of the program where that pointer may have been
passed to?
urs
function which does not modify the object, I usually use const. But
when that pointer is later copied somewhere else, e.g. a list or
another function, I need to cast the const away, which I don't like.
class A;
class B {
const A *a;
public:
B(const A *a) : a(a) {}
void foo(std::list<A *> &l) {
l.push_back((A *)a); // the cast looks ugly
}
};
What exactly is the meaning of B(const A *a); ? Is that an assurance
to the compiler that the pointed-to object is not modified until B()
returns (enabling some optimizations), or does it mean that the object
is never modified through this pointer, not even later in some
completely other part of the program where that pointer may have been
passed to?
urs