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

constness of an object

Hi,

I often think over the const_cast operator provided by C++ and I feel
usage of const_cast or the document availale for const_cast are
cumbersome.

C++ provides us, const qualifier which force a variable or an object
remain constant throughout lifetime of the program.

class Experiment
{
public:
int _member;
public setMember(int i)
{
_member = i;
}
};

void main()
{
const Experiment exp; // const object. I can't do set
}

Here my question is const_cast talks about removing this particular
const_ness of the object exp?

Experiment &e = const_cast<Experiment&> (exp);

e.setMember(10);

In the above code, it doesn't make sense to change the constant object
through the reference after the const_cast.
From my understanding, const_cast could be applied to an constant

reference to an object which is not actually a constant object. Look at
the code,

void Fun(const Experiment& obj)
{
Experient &rObj = const_cast<Experiement&> (obj);
rObj.setMember(40);
}

void main()
{

// Valid use

Experiment obj; // note not a constant
Fun(obj); // make sense
// Value of the object changed in the Fun

// Invalid use

const Experiment cObj;
Fun(cObj); // non-sense.. Am i right?
// Value of the object cObj should not changed in the Fun
}

So can some one define the what is constness?. How it can be used?

Thanks & Regards,

Gopal

Dec 9 '05 #1
3 1872
* ma********@gmail.com:

I often think over the const_cast operator provided by C++ and I feel
usage of const_cast or the document availale for const_cast are
cumbersome.

C++ provides us, const qualifier which force a variable or an object
remain constant throughout lifetime of the program.

class Experiment
{
public:
int _member;
Don't provide public data members.
<url:
http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.7>

public setMember(int i)
{
_member = i;
}
};

void main()
Not valid C++.
<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3>.
<url: http://www.research.att.com/~bs/bs_faq2.html#void-main>.
{
const Experiment exp; // const object. I can't do set
Not valid C++: a const local object must be initialized or be of a type
with a user-defined default constructor. Anyway you explicitly have to
define the value. Give your class a constructor.

}

Here my question is const_cast talks about removing this particular
const_ness of the object exp?
No. If you remove const-ness of an original const object you have
Undefined Behavior.

Experiment &e = const_cast<Experiment&> (exp);

e.setMember(10);

In the above code, it doesn't make sense to change the constant object
through the reference after the const_cast.

From my understanding, const_cast could be applied to an constant
reference to an object which is not actually a constant object.
Yep.

Look at
the code,

void Fun(const Experiment& obj)
{
Experient &rObj = const_cast<Experiement&> (obj);
rObj.setMember(40);
}

void main()
Not valid C++.
<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3>.
<url: http://www.research.att.com/~bs/bs_faq2.html#void-main>.

{

// Valid use

Experiment obj; // note not a constant
Fun(obj); // make sense
// Value of the object changed in the Fun

// Invalid use

const Experiment cObj;
Fun(cObj); // non-sense.. Am i right?
// Value of the object cObj should not changed in the Fun
}

So can some one define the what is constness?.
<url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.6>
<url: http://www.parashift.com/c++-faq-lite/const-correctness.html>

How it can be used?


Generally, avoid const_cast, C-style cast and mutable... ;-)

Then, make anything that can be const (except class data members),
const.

That way you'll catch a lot of bugs and will live a long, rich life
flush with money and _very_ attractive sexual partners.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 9 '05 #2
ma********@gmail.com wrote:

So can some one define the what is constness?. How it can be used?

There are two major forms of constness. I'll call them const
storage and const access.

When you defined the variable
const Experiment exp;
you have an object that is defined to be const. I'll call that
const storage. Any attempt to change this object through
const_cast or other ruse will result in undefined behavior.
void foo(const Experiment& e) {
const_cast<Experiment>(e).SetMemeber(4);
}

int main() {
Experiment non_const;
foo(non_const);
}

Here we have an accessor reference in this case (a pointer would
be similar) that says "we can't modify the object." However
the object it is referring to isn't really const (const storage).
Now while it's anti-social and lie and say you're not going to
modify something in foo via the declaration and go ahead and
do so anyhow, it doesn't break any language rules.
Dec 10 '05 #3
<ma********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I often think over the const_cast operator provided by C++ and I feel
usage of const_cast or the document availale for const_cast are
cumbersome.


One legitimate use of const_cast is when calling interfaces that are not
const-correct. Assume that there is this function provided by a library:

void does_not_modify_parameter(Foo & foo);

That function is broken because even though it doesn't modify the passed in
parameter, the parameter is not const.

In order to call such a function, you can use const_cast:

void my_function(Foo const & foo_const)
{
Foo & foo = const_cast<Foo &>(foo_const);
does_not_modify_parameter(foo);
}

As others have already said though, if does_not_modify_parameter attempts to
modify the parameter; it is undefined behavior.

Ali

Dec 14 '05 #4

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

Similar topics

0
by: Dave | last post by:
Hi, I just want to share what I have found today. In the online article "Enforce Constness With C#" by Bill Wagner for "Visual Studio Magazine", he explaines how to make a little workaround to...
15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
6
by: Marc | last post by:
T x; T foo(T, T); bind1st(ptr_fun(foo), x) creates a function object that takes an argument of type T const&. This does not work if T is already a reference type like int const&. So my first...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
6
by: Querejeto | last post by:
Hello: Is it possible to detect programmatically the constness of a member function when it is called? That is, I would like to see a generic implementation (i.e. it does not depend on the...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
13
by: Javier | last post by:
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...
6
by: Tarique | last post by:
Can someone please explain the idea of Logical Constness as explained in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a short working code if possible. Thank You
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...

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.