By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,237 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,237 IT Pros & Developers. It's quick & easy.

constness of an object

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
* 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

P: n/a
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

P: n/a
<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 discussion thread is closed

Replies have been disabled for this discussion.