constness of an object

M

mailtogops

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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>.
{

// 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>
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.
 
R

Ron Natalie

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.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top