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. default arguments resolution

default arguments resolution

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 3 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.
  • H Offline
    H Offline
    hawk23reddy
    wrote on last edited by
    #1

    consider the below code

    class ab
    {
    public:
    virtual void foo(int a = 2) = 0;
    };
    class ba : public ab
    {
    public:
    void foo(int a = 4){std::cout<

    this will print me 2 i.e base class signature is used so is default arguemts are resolved during complie time or runtime and why is the above doing so { but i was excepting it to be 4 (derived default)}

    J 1 Reply Last reply
    0
    • H hawk23reddy

      consider the below code

      class ab
      {
      public:
      virtual void foo(int a = 2) = 0;
      };
      class ba : public ab
      {
      public:
      void foo(int a = 4){std::cout<

      this will print me 2 i.e base class signature is used so is default arguemts are resolved during complie time or runtime and why is the above doing so { but i was excepting it to be 4 (derived default)}

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Default arguments are resolved at compile time. (This is by necessity to build the stack correctly prior to the call. Thus, once a virtual function is called through the vtable, the call stack must already be set up.)

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      H 1 Reply Last reply
      0
      • J Joe Woodbury

        Default arguments are resolved at compile time. (This is by necessity to build the stack correctly prior to the call. Thus, once a virtual function is called through the vtable, the call stack must already be set up.)

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        H Offline
        H Offline
        hawk23reddy
        wrote on last edited by
        #3

        joe, but y is it sets the argument to base function signature arguments..

        J S 2 Replies Last reply
        0
        • H hawk23reddy

          joe, but y is it sets the argument to base function signature arguments..

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Because that's all it "knows" about. The use of the virtual function isn't done (or technically "known") until runtime.

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          1 Reply Last reply
          0
          • H hawk23reddy

            joe, but y is it sets the argument to base function signature arguments..

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            The C++ standard says this about default arguments (in section 8.3.6 para 10):

            10 A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.

            Note the use of static type of the pointer or reference - the static type of your reference is a reference to the base type, so it uses the base type's default parameter value.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            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