Topic: ambiguity in overloaded member functions
Author: allan_w@my-dejanews.com (Allan W)
Date: Wed, 8 Jan 2003 05:15:10 +0000 (UTC) Raw View
> AJ Friend <aj4528@comcast.net> writes
> >But, here is my first question: If i take the same
> >two functions, and put the 'const' keyword in one of
> >the function's declaration, the compiler no longer
> >gives me an error and will now always use the
> >non-constant function in every situation. It is like
> >the const function does not exist.For example:
> >
> >int printAge()const { ... }
> >float printAge() { ... }
francis.glassborow@ntlworld.com (Francis Glassborow) wrote
> I think that if the compiler is allowing you that declaration/definition
> outside of a class scope, the compiler is broken.
I agree.
It SHOULD work within a class, though -- in case AJ Friend didn't already
know.
struct Foo {
int printAge() const { std::cout << "int\n"; return 0; }
float printAge() { std::cout << "float\n"; return 0.0; }
};
int main() {
Foo f;
const Foo g;
f.printAge(); // Should print "float"
g.printAge(); // Should print "int"
}
Here, the "thing" that is const (for the const version) is the object
itself. When the object is const (as g is), it must call the printAge()
version. When the object is not const, it will call the non-const
version instead.
In your original example , the printAge() function was not part of a
class and there was no object to be const or not. The compiler should
reject using the "const" keyword at the end of a declaration or
definition of a non-member function.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: francis.glassborow@ntlworld.com (Francis Glassborow)
Date: Sun, 5 Jan 2003 07:42:46 +0000 (UTC) Raw View
In article <pan.2003.01.03.04.05.53.917387@comcast.net>, AJ Friend
<aj4528@comcast.net> writes
>But, here is my first question: If i take the same
>two functions, and put the 'const' keyword in one of
>the function's declaration, the compiler no longer
>gives me an error and will now always use the
>non-constant function in every situation. It is like
>the const function does not exist.For example:
>
>int printAge()const
>{
> cout << "int age: " << age << endl;
> return age;
>}
I think that if the compiler is allowing you that declaration/definition
outside of a class scope, the compiler is broken.
>float printAge()
>{
> cout << "float age: " << age << endl;
> return age;
>}
>
>int main()
>{
> int i;
> float f;
>
> printAge();
> i = printAge();
> f = printAge();
>
>return 0;
>}
>
>If i run this code, the printAge() function that returns
>float is called everytime. Can anybody help me to
>understand why this happens?
As written, the compiler is broken because it should not be accepting
the overload.
>Also, what if one function
>is the member function of a base class and the other
>is an overriding function in a derived class?
You cannot override a function with another function that has a
different signature (and const member functions are different from
non-const ones)
so the derived class one hides the base class function unless you have
explicitly imported it to the derived class with a using declaration.
> With and
>without the 'const' keyword being used in this scenario,
>which function gets called in which situations and why?
Without virtual, the member function of the static type of the reference
or pointer being used, except that when not const-qualified const
instances will result in a diagnostic.
Personally I think you should be posting to some other newsgroup such as
comp.lang.c++.moderated or alt.comp.lang.learn.c-c++ because your
questions concern your understanding of the use of C++ rather than
issues with the C++ standard and its interpretation.
--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow ACCU
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: aj4528@comcast.net ("AJ Friend")
Date: Sat, 4 Jan 2003 00:23:50 +0000 (UTC) Raw View
Just trying to figure out a few things dealing with
function overloading.
First off, i find that i cannot overload a function
so that the only difference is the return type of
the function, besides the body of the function, of
course. The reason behind this seems obvious enough,
as the compiler would have no way to know which
function to call if the function was not being used
as a right hand value.
But, here is my first question: If i take the same
two functions, and put the 'const' keyword in one of
the function's declaration, the compiler no longer
gives me an error and will now always use the
non-constant function in every situation. It is like
the const function does not exist.For example:
int printAge()const
{
cout << "int age: " << age << endl;
return age;
}
float printAge()
{
cout << "float age: " << age << endl;
return age;
}
int main()
{
int i;
float f;
printAge();
i = printAge();
f = printAge();
return 0;
}
If i run this code, the printAge() function that returns
float is called everytime. Can anybody help me to
understand why this happens? Also, what if one function
is the member function of a base class and the other
is an overriding function in a derived class? With and
without the 'const' keyword being used in this scenario,
which function gets called in which situations and why?
Thanx in advance,
AJ
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]