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<chi ld_t>
{
public:
parent_t(void);
}
---------------
The problem is that I std::vector<chi ld_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::paren t_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::operat or=(const child_t& other)
{
const_cast<pare nt_t*>(parent) = other.parent;
}
gives
error: assignment of read-only data-member `child_t::paren t'
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 6 1628
"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
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<chi ld_t> { public: parent_t(void); }
---------------
The problem is that I std::vector<chi ld_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::paren t_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::operat or=(const child_t& other) { const_cast<pare nt_t*>(parent) = other.parent; }
gives
error: assignment of read-only data-member `child_t::paren t'
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<par ent_t**>(&paren t) = 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...
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
"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
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::pparen t.
Is it true or is there any cost for encapsulating parent_t* this way?
AlesD
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::pparen t.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 I'm doing
wrong. Here is the exercise:
27. Create a const array of double and a volatile array of double.
Index through each array and use const_cast to cast each element to
non-const and non-volatile, respectively, and assign a value to each...
|
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);
|
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 this a little when I converted a primitive to a class, but
would like to see other applications.
|
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 the constant
variable and if I changed the value through the pointer, the constant
variable should have been modified. Well, that's what I thought until
I wrote the following and run it.
#include <iostream>
using std::cout;
using std::endl;
|
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)
| |
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 wonder if this is dangerous. I
have something like that:
const Object &
Object::dummy(const AType &a) const
{
|
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();
}
|
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,
--------------------
|
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 done.
If mutable is only the solution, I will follow not to use const_cast.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |