473,657 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const overload

Hi Everyone:

Short description first

MathematicalSet is to be a class template that's supposed to behave as the
name suggests. It has functions that define union, contains,
is_contained_in , etc... I want the thing to behave like a normal collection
as well. That is to comply with the ansi standard for STL collections even
though it manages two separate lists and two statics (universe and null).
The first collection is an object pool within which we store copies of the
objects the user wants in his/her set each wrapped in a 'countable' element
template. The next collection is a list of countable pointers that point to
these elements in the pool. The idea being that we should only need one
distinct copy of each object from which we can build as many sets as we want
which contain that object (or copies of it) in the form of counted pointers.
Then, if the iterator for the MathematicalSet template is defined properly,
we can hand the user her/his set of counted pointers but it will behave as
if it were a set of actual objects instead. Now I've got some code written
towrd this end and it even seems to be working properly. That is, I can
declare and initialise a MathematicalSet ::const_iterato r and set it to the
beginning of the set...iterate over the set and dereference the iterator to
obtain the const reference to the object i desire. Any code that tries to
modify that object is flagged by the compiler at compile time as faulty. And
the same for MathematicalSet ::iterator except, of course, I can have code
that modifies the object pointed to by the iterator. My problem lies in how
the compiler is behaving with regard to the various access functions for the
iterators. Namely the 'begin' function. I checked in the STL def'n of
<list> used by the compiler for direction on how to define my template and
found complementary def'ns of begin() for each iterator type (const and
nonconst) and they look something like this (Note I've removed various _'s
and other decorations for readability):

iterator begin()
{
return (iterator(Acc:: Next(Head)));
}

const_iterator begin() const
{
return (const_iterator (Acc::Next(Head )));
}

so I happily set about defining my own 'begin' functions which return
iterators pointing to the beginning of my list of counted pointers in
keeping with the ideas described above. e.g.

template<object >
class MathematicalSet
{
....
class const_iterator{ // stuff }; // end const_iterator
class iterator : public const_iterator { // more stuff }; // end
iterator
.....
// access functions
iterator begin()
{
Set::iterator theStartOfTheSe t = m_TheSet.begin( );
return (iterator(theSt artOfTheSet));
}

const_iterator begin() const
{
Set::const_iter ator theStartOfTheSe t = m_TheSet.begin( );
return (const_iterator (theStartOfTheS et));
}
.....
and now I can write the following code....Note that class bob is just a
silly test class with an int and a char member (see way below for it's
decl'n/def'n if you need to)

int main()
{
bob b, b1, b2;
b2.a = 1;
MathematicalSet <bob> aSet;

aSet.add(b);
aSet.add(b);
aSet.add(b2);
aSet.add(b1);
MathematicalSet < bob >::const_iterat or it = aSet.begin();
bob c = *it;
// (*it).a =7; /* note that this line won't compile if uncommented as it is
a const_iterator */
++it;
it++;
--it;
it--;
it++, it++;
c = *it;
MathematicalSet < bob >::iterator itt = aSet.begin();
++itt, ++itt;
itt->a = 3; /* this works fine since itt is nonconst */
bob d = *itt;
return 0;
}

compiles with no errors or warnings and works splendidly, but when I step
through the code I expect to
step through both versions of the begin() function. It doesn't do this at
all, in fact it completely
ignores the const_iterator begin() const function which I expect to be
called for the first
iterator declaration. It actually moves any breakpoints I set in the
function to the 'next valid line' which
is outside of th function, according to the compiler....so. ...what's wrong
with my thinking? Any comments
or suggestions on this problem (or anything at all) are welcome.

MSVC 6.0 is the compiler though not certain at all if this is the issue.

decl'n/def'n of the silly test class

class bob
{
public:

int a;
char b;

bob():a(0),b(0) {}

bob(const bob& d)
{
a = d.a;
b = d.b;
}

bob& operator=(const bob& d)
{
if(this != &d)
{
a = d.a;
b = d.b;
}
return *this;
}

const bool operator==(cons t bob& d) const
{
return (a == d.a) ? ( b == d.b) : false;
}

~bob(){}
};

thanks, and regards,
L.
Jul 22 '05 #1
4 1802

"NKOBAYE027 " <NK********@Rog ers.Com> wrote in message
news:dc******** **************@ twister01.bloor .is.net.cable.r ogers.com...
Hi Everyone:

[snip]

int main()
{
bob b, b1, b2;
b2.a = 1;
MathematicalSet <bob> aSet;

aSet.add(b);
aSet.add(b);
aSet.add(b2);
aSet.add(b1);
MathematicalSet < bob >::const_iterat or it = aSet.begin();
bob c = *it;
// (*it).a =7; /* note that this line won't compile if uncommented as it is a const_iterator */
++it;
it++;
--it;
it--;
it++, it++;
c = *it;
MathematicalSet < bob >::iterator itt = aSet.begin();
++itt, ++itt;
itt->a = 3; /* this works fine since itt is nonconst */
bob d = *itt;
return 0;
}

compiles with no errors or warnings and works splendidly, but when I step
through the code I expect to
step through both versions of the begin() function. It doesn't do this at
all, in fact it completely
ignores the const_iterator begin() const function which I expect to be
called for the first
iterator declaration. It actually moves any breakpoints I set in the
function to the 'next valid line' which
is outside of th function, according to the compiler....so. ...what's wrong
with my thinking? Any comments
or suggestions on this problem (or anything at all) are welcome.


OK, you've misunderstood how function overloading works. The return type of
a function is *never* taken into account when deciding which version of an
overloaded function is called. So that fact that you are assigning to a
const_iterator is completely irrelevant to this situation. What counts is
whether the aSet object is const or not. In this case it isn't so the
non-const version of begin is called, returning an iterator, which is them
implicitly converted (in some way I didn't look) to a const_iterator.

This is all correct and as expected. If you did some experimentation with
other STL iterators you would see that they behave in the same way.

John

Jul 22 '05 #2
Thanks John. So the object doing the calling is the deciding factor
here...const objects would call the const overloaded function presumably
because of the trailing const in the function declaration? Grrrrrr ok...back
to Stroustrup....
"John Harrison" <jo************ *@hotmail.com> wrote in message
news:2g******** ****@uni-berlin.de...

"NKOBAYE027 " <NK********@Rog ers.Com> wrote in message
news:dc******** **************@ twister01.bloor .is.net.cable.r ogers.com...
Hi Everyone:

[snip]

int main()
{
bob b, b1, b2;
b2.a = 1;
MathematicalSet <bob> aSet;

aSet.add(b);
aSet.add(b);
aSet.add(b2);
aSet.add(b1);
MathematicalSet < bob >::const_iterat or it = aSet.begin();
bob c = *it;
// (*it).a =7; /* note that this line won't compile if uncommented as it

is
a const_iterator */
++it;
it++;
--it;
it--;
it++, it++;
c = *it;
MathematicalSet < bob >::iterator itt = aSet.begin();
++itt, ++itt;
itt->a = 3; /* this works fine since itt is nonconst */
bob d = *itt;
return 0;
}

compiles with no errors or warnings and works splendidly, but when I step through the code I expect to
step through both versions of the begin() function. It doesn't do this at all, in fact it completely
ignores the const_iterator begin() const function which I expect to be
called for the first
iterator declaration. It actually moves any breakpoints I set in the
function to the 'next valid line' which
is outside of th function, according to the compiler....so. ...what's wrong with my thinking? Any comments
or suggestions on this problem (or anything at all) are welcome.


OK, you've misunderstood how function overloading works. The return type

of a function is *never* taken into account when deciding which version of an
overloaded function is called. So that fact that you are assigning to a
const_iterator is completely irrelevant to this situation. What counts is
whether the aSet object is const or not. In this case it isn't so the
non-const version of begin is called, returning an iterator, which is them
implicitly converted (in some way I didn't look) to a const_iterator.

This is all correct and as expected. If you did some experimentation with
other STL iterators you would see that they behave in the same way.

John

Jul 22 '05 #3

"NKOBAYE027 " <NK********@Rog ers.Com> wrote in message
news:Wg******** **************@ twister01.bloor .is.net.cable.r ogers.com...
Thanks John. So the object doing the calling is the deciding factor
here...const objects would call the const overloaded function presumably
because of the trailing const in the function declaration? Grrrrrr ok...back to Stroustrup....


Right, or a when you have a const reference. In that case the const overload
would be called as well.

john
Jul 22 '05 #4
John Harrison wrote:
"NKOBAYE027 " <NK********@Rog ers.Com> wrote in message
news:Wg******** **************@ twister01.bloor .is.net.cable.r ogers.com...
Thanks John. So the object doing the calling is the deciding factor
here...cons t objects would call the const overloaded function presumably
because of the trailing const in the function declaration? Grrrrrr


ok...back
to Stroustrup....

Right, or a when you have a const reference. In that case the const overload
would be called as well.

john


Or a pointer to const.
Jul 22 '05 #5

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

Similar topics

5
9394
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts. If i make another constructor with parameter const char*, how can i call the constructor with the const string& ? I tried
4
6688
by: Jim West | last post by:
The following compiles with g++ 3.3.2, but fails with Intel icc 7.1 with the error: asdf.cc(6): error: expression must be an lvalue or a function designator f1(&arr); Is this undefined behavior, or have I found a problem with one compiler? Code sample is:
16
2463
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
7
1835
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code f(c,c,cc) calls f<char*, const char *>, which is fine. But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to call f<const char*,char*>.
13
2487
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For example, if you only have this code ... void foo( const int n ) {}
24
2320
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
19
2216
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int iVal):m_iValue(iVal){}
10
2179
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
35
34365
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
3
1138
by: =?iso-8859-1?Q?Daniel_Lidstr=F6m?= | last post by:
Hello! I have a strange problem with my pimpl implementation. What happens is that const correctness is not respected. Let me go straight to the code: #include <iostream> struct Geometry {}; class LineData
0
8821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.