A
AlesD
Hello,
I can't figure out how to build assignment operator for class which
contains "type* const value".
--- example ---
class parent_t;
class child_t {
private:
friend class parent_t;
// Private c'tor so only parent_t can create child_t
child_t(parent_t* iparent): parent(iparent) {};
public:
parent_t* const parent;
}
class parent_t: std::vector<child_t>
{
public:
parent_t(void);
}
---------------
The problem is that I std::vector<child_t> complains that:
error: non-static const member `
parent_t* const child_t:arent' can't use default assignment operator
and copy constructor of parent_t:
parent_t:arent_t(const parent_t& other)
{
iterator i = begin();
while (i != end())
i->parent = this;
}
needs to point it's copies of child_t to itself.
I wanted to resolve this by using const_cast, but I can't figure how to
change `parent_t* const' to `parent_t*'. Following code
child_t&
child_t:perator=(const child_t& other)
{
const_cast<parent_t*>(parent) = other.parent;
}
gives
error: assignment of read-only data-member `child_t:arent'
My book on C++ nor "C++ FAQ Lite" mention this case. Is it possible to
do it?
BTW: Even better would be if I could define child_t as
class child_t {
private:
friend class parent_t;
child_t(parent_t* iparent): parent(iparent) {};
public:
parent_t& parent; // <-- reference instead of `* const'
}
but I think this is impossible since there is no way how to reseat
reference.
Thanks in advance - Ales
I can't figure out how to build assignment operator for class which
contains "type* const value".
--- example ---
class parent_t;
class child_t {
private:
friend class parent_t;
// Private c'tor so only parent_t can create child_t
child_t(parent_t* iparent): parent(iparent) {};
public:
parent_t* const parent;
}
class parent_t: std::vector<child_t>
{
public:
parent_t(void);
}
---------------
The problem is that I std::vector<child_t> complains that:
error: non-static const member `
parent_t* const child_t:arent' can't use default assignment operator
and copy constructor of parent_t:
parent_t:arent_t(const parent_t& other)
{
iterator i = begin();
while (i != end())
i->parent = this;
}
needs to point it's copies of child_t to itself.
I wanted to resolve this by using const_cast, but I can't figure how to
change `parent_t* const' to `parent_t*'. Following code
child_t&
child_t:perator=(const child_t& other)
{
const_cast<parent_t*>(parent) = other.parent;
}
gives
error: assignment of read-only data-member `child_t:arent'
My book on C++ nor "C++ FAQ Lite" mention this case. Is it possible to
do it?
BTW: Even better would be if I could define child_t as
class child_t {
private:
friend class parent_t;
child_t(parent_t* iparent): parent(iparent) {};
public:
parent_t& parent; // <-- reference instead of `* const'
}
but I think this is impossible since there is no way how to reseat
reference.
Thanks in advance - Ales