Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. overloading across scopes

overloading across scopes

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vikramlinux
    wrote on last edited by
    #1

    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?

    4 V C 3 Replies Last reply
    0
    • V vikramlinux

      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?

      4 Offline
      4 Offline
      4apai
      wrote on last edited by
      #2

      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.

      V 1 Reply Last reply
      0
      • 4 4apai

        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.

        V Offline
        V Offline
        vikramlinux
        wrote on last edited by
        #3

        No that is not What I Expect...Please read the question carefully.

        1 Reply Last reply
        0
        • V vikramlinux

          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?

          V Offline
          V Offline
          vishalmore
          wrote on last edited by
          #4

          C++ allows specification of more than one function of the same name in the same scope. These are called “overloaded functions”. :-D Cheers, Vishal

          1 Reply Last reply
          0
          • V vikramlinux

            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 Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            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]

            V 1 Reply Last reply
            0
            • C Chris Meech

              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]

              V Offline
              V Offline
              vikramlinux
              wrote on last edited by
              #6

              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?

              C 1 Reply Last reply
              0
              • V vikramlinux

                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?

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #7

                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]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups