J
Javier
Hello,
I have some cuestions about constness with standard containers and
pointers.
Supose I have a list of pointers to some class B:
std::list< B * > > list;
I have readed that constness in std::list is the same that it is in C
arrays (const std::list makes const both the list and its content):
const std::list< B * > > constList;
I can't do:
constList->push_back(x);
But, is it still possible to call any non-const member funcion of B?:
void B::nonConstMemberFuncion();
As in:
constList.begin()->nonConstMemberFuncion();
a) It is possible. I don't want the user to add elements nor modify
any of the list. Which is the correct way of doing this?
const std::list< const B * const > > constList;
const std::list< const B * > constList;
b) It is not possible. Then, I suppose that the next sentences are the
same:
const std::list < B > constList;
const std::list < const B > constList;
If I have the next class:
class A {
public:
std::list< B * > & list() { return m_list; };
private:
std::list< B * > m_list;
};
How I add a const version for A::list()?
What about if I have a smart_pointer instead of a C pointer?
const std::list< boost::shared_ptr< B > > constList =
something();
constList.begin()->nonConstMemberFunction();
Thanks.
I have some cuestions about constness with standard containers and
pointers.
Supose I have a list of pointers to some class B:
std::list< B * > > list;
I have readed that constness in std::list is the same that it is in C
arrays (const std::list makes const both the list and its content):
const std::list< B * > > constList;
I can't do:
constList->push_back(x);
But, is it still possible to call any non-const member funcion of B?:
void B::nonConstMemberFuncion();
As in:
constList.begin()->nonConstMemberFuncion();
a) It is possible. I don't want the user to add elements nor modify
any of the list. Which is the correct way of doing this?
const std::list< const B * const > > constList;
const std::list< const B * > constList;
b) It is not possible. Then, I suppose that the next sentences are the
same:
const std::list < B > constList;
const std::list < const B > constList;
If I have the next class:
class A {
public:
std::list< B * > & list() { return m_list; };
private:
std::list< B * > m_list;
};
How I add a const version for A::list()?
What about if I have a smart_pointer instead of a C pointer?
const std::list< boost::shared_ptr< B > > constList =
something();
constList.begin()->nonConstMemberFunction();
Thanks.