473,378 Members | 1,495 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,378 software developers and data experts.

Removing const for benefit of assignment operator

Should I remove const from a private member just for the sake of the
assignment operator?

I have a class that looks something like this.

class Foo
{
public:
Foo(const std::string& description) : _description(description) {}

private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change unless you
assign a new object to the Foo-instance. Does that warrant making
_description non-const.

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor grasp
of concept on my side?

Thanks in advance.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Jul 22 '05 #1
14 1764

"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:41***********************@news.xs4all.nl...
Should I remove const from a private member just for the sake of the
assignment operator?

I have a class that looks something like this.

class Foo
{
public:
Foo(const std::string& description) : _description(description) {}

private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change unless you
assign a new object to the Foo-instance. Does that warrant making
_description non-const.

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor grasp
of concept on my side?

Yes. :-) An assignment is a copy operation. The assignment operator needs
to copy into each member of your object from the right-hand-side object, and
it can't do that in this case because the string is const. If you're going
to allow assignment to your object at all, then you'll have to be able to
change _description. (As you said, it WILL change if you assign to it.)
Thus, it shouldn't be const.

-Howard
Thanks in advance.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/

Jul 22 '05 #2
Joost Ronkes Agerbeek wrote:
Should I remove const from a private member
just for the sake of the assignment operator?

I have a class that looks something like this.

class Foo {
public:
Foo(const std::string& description): _description(description) { }
private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change
unless you assign a new object to the Foo-instance.
Does that warrant making _description non-const. cat Foo.cc #include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }
Foo(const std::string& description):
_description(description) { }
};

int main(int argc, char* argv[]) {
Foo a(std::string("a"));
std::cout << a << std::endl;
a = Foo(std::string("b"));
std::cout << a << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o Foo Foo.cc
./Foo a
a

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object.
Is that a poor grasp of concept on my side?


Yes.
Jul 22 '05 #3

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:co**********@nntp1.jpl.nasa.gov...
Joost Ronkes Agerbeek wrote:
Should I remove const from a private member
just for the sake of the assignment operator?

I have a class that looks something like this.

class Foo {
public:
Foo(const std::string& description): _description(description) { }
private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change
unless you assign a new object to the Foo-instance.
Does that warrant making _description non-const.
> cat Foo.cc

#include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }


How does this help anything? No assignment is taking place here. In fact,
f is completely ignored! I don't think that's what he wanted... do you?
Foo(const std::string& description):
_description(description) { }
};

int main(int argc, char* argv[]) {
Foo a(std::string("a"));
std::cout << a << std::endl;
a = Foo(std::string("b"));
But since operator= simply returns *this, which is just a itself, how can
this line constitute an assignment of the temporary to a? The variable a
shouldn't change at all here, right?
std::cout << a << std::endl;
return 0;
}
> g++ -Wall -ansi -pedantic -o Foo Foo.cc
> ./Foo

a
a

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor
grasp of concept on my side?


Yes.

Jul 22 '05 #4
Howard wrote:
E. Robert Tisdale wrote:
Joost Ronkes Agerbeek wrote:
Should I remove const from a private member
just for the sake of the assignment operator?

I have a class that looks something like this.

class Foo {
public:
Foo(const std::string& description): _description(description) { }
private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change
unless you assign a new object to the Foo-instance.
Does that warrant making _description non-const.
> cat Foo.cc

#include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }

How does this help anything? No assignment is taking place here.
In fact, f is completely ignored!
I don't think that's what [Joost] wanted... do you?


I don't think Joost knows what he wanted
and I don't think that you know either.
Foo(const std::string& description):
_description(description) { }
};

int main(int argc, char* argv[]) {
Foo a(std::string("a"));
std::cout << a << std::endl;
a = Foo(std::string("b"));

But since operator= simply returns *this, which is just a itself,
how can this line constitute an assignment of the temporary to a?
The variable a shouldn't change at all here, right?


I's common practice to include a [const] string in an object
which describes the *variable* and not the value which it contains.
For example:

Foo name(std::string("name"));

Usually, the object contains other [private] data members
which represent the [mutable] value of the variable
so the class developer must provide an *explicit*
assignment operator which assigns a new value to the variable
without changing the description of the variable itself.
std::cout << a << std::endl;
return 0;
}
> g++ -Wall -ansi -pedantic -o Foo Foo.cc
> ./Foo

a
a

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor
grasp of concept on my side?


Yes.


Jul 22 '05 #5
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:41***********************@news.xs4all.nl...
Should I remove const from a private member just for the sake of the
assignment operator?

I have a class that looks something like this.

class Foo
{
public:
Foo(const std::string& description) : _description(description) {}

private:
const std::string _description;
};

The compiler warns me that it cannot generate an assignment operator,
because _description is const. _description will never change unless you
assign a new object to the Foo-instance. Does that warrant making
_description non-const.

Somehow I see oldFoo = newFoo more as assigning a new object to the
oldFoo-identifier than as changing the oldFoo-object. Is that a poor grasp
of concept on my side?

Thanks in advance.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/


I can't see any reason to declare _description as const. You have done your
duty by declaring it private. If you write

Foo x("abc");
Foo y("def");
y = x;

wouldn't you expect y._description to be "abc" now? If so, it would be
illogical and self defeating to declare it const.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #6
Cy Edmunds wrote:
I can't see any reason to declare _description as const.
You have done your duty by declaring it private.


You are assuming that
_description is the value of the object.
const data members are common in C++ objects.
They are often used as appears in this case
to describe the variable itself and *not* its value.
Jul 22 '05 #7
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:co**********@nntp1.jpl.nasa.gov...
Cy Edmunds wrote:
I can't see any reason to declare _description as const. You have done
your duty by declaring it private.
You are assuming that
_description is the value of the object.


No, I am assuming that _description is part of the object's abstract state.
const data members are common in C++ objects.
Maybe in objects *you* design. I don't actually recall ever using a const
data member.
They are often used as appears in this case
to describe the variable itself and *not* its value.


If a member variable is part of the abstract state of the object then
assignment should change it. If it is not part of the abstract state of the
object it should be static. I might see exceptions to this is if you are
doing some sort of debugging or logging of history, etc., but I certainly
don't find that such scenarios are particularly common in practice.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #8

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> skrev i en meddelelse
news:co**********@nntp1.jpl.nasa.gov...
Cy Edmunds wrote:
I can't see any reason to declare _description as const. You have done
your duty by declaring it private.


You are assuming that
_description is the value of the object.
const data members are common in C++ objects.
They are often used as appears in this case
to describe the variable itself and *not* its value.


I've never created a class with a const member variable. Never. Neither have
i ever seen one in one of the textbooks, I've read. So to say that they are
common certainly is far out.

/Peter
Jul 22 '05 #9

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:co**********@nntp1.jpl.nasa.gov...
Howard wrote:
E. Robert Tisdale wrote:

> cat Foo.cc
#include <string>
#include <iostream>

class Foo {
private:
const std::string _description;
public:
friend
std::ostream& operator<<(std::ostream& os, const Foo& f) {
return os << f._description;
}
Foo& operator=(const Foo& f) { return *this; }

How does this help anything? No assignment is taking place here.
In fact, f is completely ignored!
I don't think that's what [Joost] wanted... do you?


I don't think Joost knows what he wanted
and I don't think that you know either.


Well, I think I do, given the original question. From the original post:

"_description will never change unless you
assign a new object to the Foo-instance"

This implies that if you do an assignment, you get a new value for
_description. That's what the "unless" means.

-Howard


Jul 22 '05 #10
> I can't see any reason to declare _description as const. You have done
your duty by declaring it private. If you write

Foo x("abc");
Foo y("def");
y = x;

wouldn't you expect y._description to be "abc" now? If so, it would be
illogical and self defeating to declare it const.


Goodness, there's a clear example if ever I saw one. :-) Thanks for clearing
that up. Now I wonder if there is ever a situation in which you would
actually want to make a member const, but I see that there is some
discussion on that subject already.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Jul 22 '05 #11
> Yes. :-) An assignment is a copy operation. The assignment operator
needs to copy into each member of your object from the right-hand-side
object, and it can't do that in this case because the string is const. If
you're going to allow assignment to your object at all, then you'll have
to be able to change _description. (As you said, it WILL change if you
assign to it.) Thus, it shouldn't be const.

-Howard


Thanks, I think I get it now. I got a bit carried away trying to be const
correct.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Jul 22 '05 #12
>> How does this help anything? No assignment is taking place here.
In fact, f is completely ignored!
I don't think that's what [Joost] wanted... do you?


I don't think Joost knows what he wanted
and I don't think that you know either.

Well, that's partly true. After all, I wouldn't ask the question if I had
perfect understanding of the subject. :-) What I was thinking of, was a
property of the object that gets set at object construction and that will
not change during the objects life-time. When the object gets copy, the
property gets copied also. Something like: when a child is born, its gender
is decided for the duration of its life and if you clone the child, the
clone has the same gender.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Jul 22 '05 #13
Joost Ronkes Agerbeek wrote:
How does this help anything? No assignment is taking place here.
In fact, f is completely ignored!
I don't think that's what [Joost] wanted... do you?


I don't think Joost knows what he wanted
and I don't think that you know either.


Well, that's partly true. After all, I wouldn't ask the question if I had
perfect understanding of the subject. :-) What I was thinking of, was a
property of the object that gets set at object construction and that will
not change during the objects life-time. When the object gets copy, the
property gets copied also. Something like: when a child is born, its gender
is decided for the duration of its life and if you clone the child,
the clone has the same gender.


You are confusing the copy constructor and the assignement operator.

Foo brother(std::string("boy"));
Foo sister(std::string("girl"));
Foo bubba(brother); // [default] copy constructor
sister = brother; // [explicit] assignment operator

Do you want your assignment operator
to turn your sister into a boy?
Jul 22 '05 #14
> You are confusing the copy constructor and the assignement operator.

Foo brother(std::string("boy"));
Foo sister(std::string("girl"));
Foo bubba(brother); // [default] copy constructor
sister = brother; // [explicit] assignment operator

Do you want your assignment operator
to turn your sister into a boy?


Good point. Actually, I don't know whether I'd want that; I guess the
metaphore breaks down there. :-) But now I know where my confusion comes
from. Thanks.

--
Joost Ronkes Agerbeek
Yellow Wood Studios
http://www.yellowwoodstudios.com/
http://www.ronkes.nl/
Jul 22 '05 #15

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

Similar topics

3
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
6
by: AlesD | last post by:
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 {
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.