How to understand this sentence

S

stonny

I read from a book the following words:

"Const and non-const varieties of a function with the same name and
parameter list are different functions. In other words, you can overload
a function using const".

But when you call a function, how can you indicate the function you call
is const or not?

Thanks
 
P

Phlip

stonny said:
"Const and non-const varieties of a function with the same name and
parameter list are different functions. In other words, you can overload a
function using const".

But when you call a function, how can you indicate the function you call
is const or not?

Object nonConst;
Object const Const;
Object const & constRef = nonConst;

nonConst.foo(); // <-- calls foo() (non-const) if available
Const.foo(); // <-- must call foo() const
constRef.foo(); // <-- must call foo() const

Now note that constancy is a form of encapsulation. Both the Const object
and the constRef make their Object more encapsulated - meaning potentially
safer and more robust - than the non-constant versions. So "const-correct"
code generally extends more usages to constant things than to changable
things.
 
V

Victor Bazarov

stonny said:
I read from a book the following words:

"Const and non-const varieties of a function with the same name and
parameter list are different functions. In other words, you can
overload a function using const".

But when you call a function, how can you indicate the function you
call is const or not?

You don't. For a const object the const function will be called, for
a non-const object the non-const function (if it exists) will be called.

V
 
F

Frederick Gotham

stonny posted:
But when you call a function, how can you indicate the function you call
is const or not?


It's all about the "this" pointer.

If you have a class with a member function as follows:

class MyClass {
public:

int i;

void Func() { i = 5; }
};

Then it operates under-the-hood as if you wrote it like:

struct MyClass {
int i;
};

void Func(MyClass *const this) { this->i = 5; }

As you can see in the line immediately above, the "this" pointer is a
"pointer to non-const". If we expand the example to the following:

class MyClass {
public:

int i;

void Func() { i = 5; }

void Func() const {}
};

Then again, we can say it operates like:

struct MyClass {
int i;
};

void Func(MyClass *const this) { this->i = 5; }

void Func(MyClass const *const this) {}

We can see that we have function overloading. The function called depends
on whether "this" is a "pointer to const" or "pointer to non-const".

The non-const function is called if you invoke the function on:

(1) A non-const object.
(2) A reference to non-const.
(3) A pointer to non-const.

The const function is called if you invoke the function on:

(1) A const object.
(2) A reference to const.
(3) A pointer to const.

To answer your question: If you have a non-const object, and wish to invoke
a const method upon it, then choose either of:

const_cast<MyClass const &>(obj).Func();

Or:

MyClass const &cr = obj;

cr.Func();
 
L

Luc The Perverse

Phlip said:
Object nonConst;
Object const Const;
Object const & constRef = nonConst;

nonConst.foo(); // <-- calls foo() (non-const) if available
Const.foo(); // <-- must call foo() const
constRef.foo(); // <-- must call foo() const

Now note that constancy is a form of encapsulation. Both the Const object
and the constRef make their Object more encapsulated - meaning potentially
safer and more robust - than the non-constant versions. So "const-correct"
code generally extends more usages to constant things than to changable
things.

When put that way it makes a lot of sense.
 
N

Noah Roberts

stonny said:
I read from a book the following words:

"Const and non-const varieties of a function with the same name and
parameter list are different functions. In other words, you can overload
a function using const".

But when you call a function, how can you indicate the function you call
is const or not?

You usually don't. However, in some rare cases you can make a
const_cast to do so. For instance, if you have a const and non const
version of a pure virtual function then both have to be defined in all
subclasses, even if some don't do anything different in the two.
Instead of duplicating code the non const function just calls the const
version through a const cast of the this ptr. This should never make
it outside the class in question and doesn't often come up.

virtual void MyObject::f() {
const_cast<const MyObject*>(this)->f();
}

Never, ever do it the other way:

virtual void MyObject::f() const {
const_cast<MyObject*>(this)->f();
}
 
J

Jerry Coffin

I read from a book the following words:

"Const and non-const varieties of a function with the same name and
parameter list are different functions. In other words, you can overload
a function using const".

But when you call a function, how can you indicate the function you call
is const or not?

By the const-ness of the object upon which it's being invoked. E.g.:

void myfunc(someobject const &o) {
o.whatever(); // invokes o::whatever() const
}

void myotherfunc(someobject /* not const */ &o) {
o.whatever(); // invokes o::whatever()
}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top