overloading across scopes
-
In C++, there is no overloading across scopes , Please see following example #include using namespace std; class B { public: int f(int i) { cout << "f(int): "; return i+1; } // ... }; class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } // ... }; int main() { D* pd = new D; cout << pd->f(2) << '\n'; cout << pd->f(2.3) << '\n'; } which will produce: f(double): 3.3 f(double): 3.6 rather than the f(int): 3 f(double): 3.6 Why it is so?
-
In C++, there is no overloading across scopes , Please see following example #include using namespace std; class B { public: int f(int i) { cout << "f(int): "; return i+1; } // ... }; class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } // ... }; int main() { D* pd = new D; cout << pd->f(2) << '\n'; cout << pd->f(2.3) << '\n'; } which will produce: f(double): 3.3 f(double): 3.6 rather than the f(int): 3 f(double): 3.6 Why it is so?
to gain required result simply declare functions with the different argument types in one class... perhaps u mixed up overloading with overriding class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } int f(int i) { cout << "f(int): "; return i+1; } // ... }; 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.
-
to gain required result simply declare functions with the different argument types in one class... perhaps u mixed up overloading with overriding class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } int f(int i) { cout << "f(int): "; return i+1; } // ... }; 4apai There're no impossible tasks. There're tasks that required infinite period of execution time.
No that is not What I Expect...Please read the question carefully.
-
In C++, there is no overloading across scopes , Please see following example #include using namespace std; class B { public: int f(int i) { cout << "f(int): "; return i+1; } // ... }; class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } // ... }; int main() { D* pd = new D; cout << pd->f(2) << '\n'; cout << pd->f(2.3) << '\n'; } which will produce: f(double): 3.3 f(double): 3.6 rather than the f(int): 3 f(double): 3.6 Why it is so?
C++ allows specification of more than one function of the same name in the same scope. These are called “overloaded functions”. :-D Cheers, Vishal
-
In C++, there is no overloading across scopes , Please see following example #include using namespace std; class B { public: int f(int i) { cout << "f(int): "; return i+1; } // ... }; class D : public B { public: double f(double d) { cout << "f(double): "; return d+1.3; } // ... }; int main() { D* pd = new D; cout << pd->f(2) << '\n'; cout << pd->f(2.3) << '\n'; } which will produce: f(double): 3.3 f(double): 3.6 rather than the f(int): 3 f(double): 3.6 Why it is so?
You may expect the f(int) method to be called, but in resolving the call I suspect the compiler 'sees' that by casting the int to a double, it can resolve to a method within the class. Have you tried either of these?
cout << pd->f((int)2) << '\n';
cout <<((B*)(pd))->f(2) << '\n';Chris Meech I am Canadian. [heard in a local bar] Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]
-
You may expect the f(int) method to be called, but in resolving the call I suspect the compiler 'sees' that by casting the int to a double, it can resolve to a method within the class. Have you tried either of these?
cout << pd->f((int)2) << '\n';
cout <<((B*)(pd))->f(2) << '\n';Chris Meech I am Canadian. [heard in a local bar] Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]
Hi All, My question is not how to overcome the above problem. I need to know why C++ dosen't allow the overloading across the scopes?
-
Hi All, My question is not how to overcome the above problem. I need to know why C++ dosen't allow the overloading across the scopes?
vikrams wrote: overloading across the scopes? I need to understand what you mean by the above. :) Chris Meech I am Canadian. [heard in a local bar] Gently arching his fishing rod back he moves the tip forward in a gentle arch releasing the line.... kersplunk [Doug Goulden] Nice sig! [Tim Deveaux on Matt Newman's sig with a quote from me]