473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

const_cast of `T* const t' to `T* t'

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::parent' can't use default assignment operator

and copy constructor of parent_t:

parent_t::parent_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::operator=(const child_t& other)
{
const_cast<parent_t*>(parent) = other.parent;
}

gives

error: assignment of read-only data-member `child_t::parent'

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
Jul 22 '05 #1
6 1606

"AlesD" <al****@seznam.cz> wrote in message
news:cc**********@mrazik2.dkm.cz...
Hello,

I can't figure out how to build assignment operator for class which
contains "type* const value".


I think you are trying to solve the wrong problem, just don't declare it
const. Seems the only thing you gain by doing that is a headache.

john
Jul 22 '05 #2
AlesD wrote:
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::parent' can't use default assignment
operator
That's because the default assignment operator will do a memberwise
assignment. child_t::parent is constant, and you can't assign to
constants (which is actually why you make something const in the first
place).

and copy constructor of parent_t:

parent_t::parent_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.
If you want to modify the parent member, you shouldn't make it const.
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::operator=(const child_t& other)
{
const_cast<parent_t*>(parent) = other.parent;
}

gives

error: assignment of read-only data-member `child_t::parent'
const_cast is a tool that is usually used to work around design or
implementation errors in code you don't control. In your case, you
should just drop the const and all the problems are gone, or
alternatively, forbid any reparenting or assignment of your class.
My book on C++ nor "C++ FAQ Lite" mention this case. Is it possible to
do it?
*const_cast<parent_t**>(&parent) = other.parent;

But that's very ugly.
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.


Well, there is one, but you don't really wanna know...

Jul 22 '05 #3
John Harrison wrote:
"AlesD" <al****@seznam.cz> wrote in message
news:cc**********@mrazik2.dkm.cz...
Hello,

I can't figure out how to build assignment operator for class which
contains "type* const value".

I think you are trying to solve the wrong problem, just don't declare it
const. Seems the only thing you gain by doing that is a headache.

john


Sorry, but it does not help. I want to let users of child_t change
parent_t via child_t::parent, which would not be possible if I declared
it as

const parent_t* parent

but don't want to let them point it to different instance of parent,
which would be possible if I declared it as

parent_t* parent

Or am I wrong?

Ales
Jul 22 '05 #4

"AlesD" <al****@seznam.cz> wrote in message
news:cc**********@mrazik2.dkm.cz...
John Harrison wrote:
"AlesD" <al****@seznam.cz> wrote in message
news:cc**********@mrazik2.dkm.cz...
Hello,

I can't figure out how to build assignment operator for class which
contains "type* const value".

I think you are trying to solve the wrong problem, just don't declare it
const. Seems the only thing you gain by doing that is a headache.

john


Sorry, but it does not help. I want to let users of child_t change
parent_t via child_t::parent, which would not be possible if I declared
it as

const parent_t* parent

but don't want to let them point it to different instance of parent,
which would be possible if I declared it as

parent_t* parent

Or am I wrong?

Ales


Well don't declare parent as public (public data is almost always a bad idea
anyway). Write

class child_t
{
public:
parent_t* get_parent() const
{
return parent;
}
private:
parent_t* parent;
};

The absence of a set_parent method will stop the users of child_t from
changing parent.

john
Jul 22 '05 #5
Rolf Magnus wrote:
const_cast is a tool that is usually used to work around design or
implementation errors in code you don't control. In your case, you
should just drop the const and all the problems are gone, or
alternatively, forbid any reparenting or assignment of your class.


You are right - I found solution which is clear:

class child_t
{
private:
parent_t* pparent;
public:
parent_t* parent(void) {return parent;};
}

This way I the users of child_t can modify parent, but can't "assign"
child to different parent.

My last question is about efficiency. I suppose that good compiler (g++
??) would inline child_t::parent() and optimize it that calling it will
be equal to direct use of child_t::pparent.

Is it true or is there any cost for encapsulating parent_t* this way?

AlesD
Jul 22 '05 #6
AlesD wrote:
Rolf Magnus wrote:
const_cast is a tool that is usually used to work around design or
implementation errors in code you don't control. In your case, you
should just drop the const and all the problems are gone, or
alternatively, forbid any reparenting or assignment of your class.
You are right - I found solution which is clear:

class child_t
{
private:
parent_t* pparent;
public:
parent_t* parent(void) {return parent;};


Drop the semicolon after the curly brace.
}
Add a semicolon after the curly brace. ;-)

This way I the users of child_t can modify parent, but can't "assign"
child to different parent.
Yes, that's the typical solution to it.

My last question is about efficiency. I suppose that good compiler
(g++ ??) would inline child_t::parent() and optimize it that calling
it will be equal to direct use of child_t::pparent.


Yes. I'd expect the code that the compiler produces to be the same as if
the member was directly accessed, but it might depend on the
optimization level you set.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: drowned | last post by:
all right, check it out... I've got a practice exercise from "thinking in c++" whose answer isn't covered in the annotated solutions guide, so I'm trying to handle it, but I don't understand what...
4
by: S.Senthilvel | last post by:
hi, I am a little confused about the const_cast.I've thought it this way till now. //Proper cast int i = 10; const int* pci = &i; int *pi = const_cast<int*>(pci);
18
by: johny smith | last post by:
Can someone please give me some good reference site/information for understanding when and why I would use 1.) const_cast Don't know why you would do this? 2.) reinterpret_cast. I have used...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
6
by: Alexander Stippler | last post by:
Hello, I wonder if and in what cases casting const away by a const_cast can be dangerous. In one place the only way I can imagine to realize a pretty nice feature is casting away const. Now I...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
2
by: George2 | last post by:
Hello everyone, In MSDN sample for const_cast, http://msdn2.microsoft.com/en-us/library/bz6at95h(VS.80).aspx There is a statement like this, --------------------
1
by: Immortal Nephi | last post by:
How can I modify member variable inside class if member function has const like mem_Func(void) const. Please do not offer the keyword -- mutable. I want to know if keyword -- const_cast can be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.