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

How Complier Distinguish Between const_iterator begin() and iterator begin()

Hello All,

I have a very basic question on STL Iterator implementation, I like to know :

How does complier distinguish between const_iterator begin() and iterator begin().

There are few answer's on the internet but nothing concrete. I can't find exact, might be using wrong keywords in search.

Can someone point out it, if possible with example.

Many Thanks
Sep 16 '10 #1

✓ answered by Banfa

It's not

const_interator begin();

it's

const_interator begin() const;

and that trailing const makes all the difference because you could not have both overloads

const_interator begin();
interator begin()

the compiler can not distinguish between them (since return type plays no part in distinguishing between overloads) but you can have both overloads

const_interator begin() const;
interator begin()

because the compiler can distinguish between them because of the trailing const on the first definition which the compiler does use in distinguishing overloads.

The compiler then uses the context that begin is called in to decide which of the 2 begins to call calling

const_interator begin() const;

if the context is constant and

interator begin();

otherwise.

5 2961
Banfa
9,065 Expert Mod 8TB
It's not

const_interator begin();

it's

const_interator begin() const;

and that trailing const makes all the difference because you could not have both overloads

const_interator begin();
interator begin()

the compiler can not distinguish between them (since return type plays no part in distinguishing between overloads) but you can have both overloads

const_interator begin() const;
interator begin()

because the compiler can distinguish between them because of the trailing const on the first definition which the compiler does use in distinguishing overloads.

The compiler then uses the context that begin is called in to decide which of the 2 begins to call calling

const_interator begin() const;

if the context is constant and

interator begin();

otherwise.
Sep 16 '10 #2
Thanks for looking into, this sample code -
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     int l;
  4.     const int m;
  5. public:
  6.     A() : l(10), m(15){};
  7.     const A* function(void) const
  8.     {
  9.         cout<<"Const "<<endl;
  10.         return this;
  11.     }
  12.  
  13.     A* function(void)
  14.     {
  15.         cout<<"Non Const "<<endl;
  16.         return this;
  17.     }
  18. };
  19.  
  20. int main()
  21. {
  22.     A a;
  23.     const A *a = a.function();
  24.     return 0;
  25. }
  26.  
Never call the first member function i.e. const A* function(void) const. What change I need to do to make it work.
Sep 16 '10 #3
Banfa
9,065 Expert Mod 8TB
Well you are not calling A::function in a constant context so it is defaulting to calling the non-const A::function because the conversion A* -> const A* is completely valid.

Why do you need it to call const A* function(void) const? It would be normal practice for 2 functions like this, 1 const 1 not const to do more or less the same thing.
Sep 16 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Iterators in the STL are nested classes:

Expand|Select|Wrap|Line Numbers
  1. class X
  2. {
  3.     public:
  4.       class const_iterator
  5.       {
  6.  
  7.       };
  8.  
  9. };
  10.  
  11. class Y
  12. {
  13.     public:
  14.       class const_iterator
  15.       {
  16.  
  17.       };
  18.  
  19. };
The compiler sees X::const_oterator as a separate object from Y::const_iterator.

A begin() method returns an X::iterator or a Y::iterator. To get that returned iterator into a const_iterator object, you will need an X::const_iterator::operator=(X::terator&) function and a
Y::const_iterator::operator=(Y::terator&) function.
Sep 16 '10 #5
Oralloy
988 Expert 512MB
Does your example even compile? I see the variable a as being defined twice.

Regardless, you can force const-ness with a cast, but I do not think that it's appropriate to do so.

C++ does not infer function signature from expected return type, it infers function signature from the argument types. There is a whole argument/discussion in the OOP world about selecting overloaded functions based on return type. Some languages do, others don't.

In your case, the implicit this argument is a non-const, as the variable a is not const. Since the only argument to function is implicitly this, in your case, the non-const form is selected.

You might try changing main to read like this:
Expand|Select|Wrap|Line Numbers
  1. int main() 
  2.     A a;
  3.     A const &b = a;
  4.     const A *c = b.function(); 
  5.     return 0; 
Sep 16 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
0
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
11
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
4
by: Arturo Cuebas | last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa) begin/end pairs that point to various chunks in a file. It would be super-cool, in my program, to be able to treat all of these ranges...
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
4
by: hokus | last post by:
I have the following class: <CODE> class List { public: class Node {
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
5
by: maverik | last post by:
Hi all. I'm implementing class AbstractCollection - just a wrap on std::vector - to hide from clients the fact of using std::vector in the base (because in the future I may change vector to a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.