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

How to understand this sentence

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
Aug 3 '06 #1
6 1188
stonny wrote:
"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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 3 '06 #2
stonny wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 3 '06 #3
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();

--

Frederick Gotham
Aug 3 '06 #4
"Phlip" <ph******@yahoo.comwrote in message
news:d5****************@newssvr13.news.prodigy.com ...
stonny wrote:
>"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.
When put that way it makes a lot of sense.

--
LTP

:)
Aug 3 '06 #5

stonny wrote:
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();
}

Aug 3 '06 #6
In article <J3********@bath.ac.uk>, ee***@bath.ac.uk says...
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()
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 3 '06 #7

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

Similar topics

1
by: Christian Buck | last post by:
Hi, i'm writing a regexp that matches complete sentences in a german text, and correctly ignores abbrevations. Here is a very simplified version of it, as soon as it works i could post the...
10
by: Ed | last post by:
Hoping someone an assist me urgently. I would truly appreciate and help. I have a form and prices are based on 'price break'. 1.The price break fields work fine, but I cannot for the life of me...
33
by: bissatch | last post by:
Hi, I fully understand the purpose of an alt attribute within a <img> tag but why would you use a title or summary attribute within, for example, a <p> tag. I have read books recommending that I...
12
by: mrby | last post by:
Hi, I was reading section 3 of the C FAQ: http://www.eskimo.com/~scs/C-faq/top.html I need more information about question 3.8: ================== 3.8: How can I understand these complex...
5
by: Antoine | last post by:
Hi I have a novice question. I am writing some code, and several routines in ..NET. So far very succesfully using several sample to work from, though am I jumping in by doing this and I really need...
10
by: Sidhu | last post by:
Hoe to relplace the word in sentence? Can any one send program?
6
by: mike | last post by:
Hello, I am trying to write some code to parse a sentence and hyperlink just the words in it. I used Aaron's code from an earlier question as a start. So far, all the code does below is...
4
by: wohast | last post by:
Hi, I'm stuck at the very beginning of my current assignment. I just need a little help getting started, and then I know how to do everything else. ask user: "Please enter your name followed by...
19
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
1
by: fellya | last post by:
Hi, i don't have enough experience in writing codes in Python but now i'm trying to see how i can start using Python. I've tried to write a simple program that can display a sentence. now my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.