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. who can tell me WHY?

who can tell me WHY?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 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.
  • S Offline
    S Offline
    simon wan
    wrote on last edited by
    #1

    its output is 47, why? #include "stdafx.h" #include class base { int i; public: base(base&){} base(int I=0):i(I){} virtual int sum(){return i;} }; class derived:public base { int j; public: derived(derived&){} derived(int I=0, int J=0):base(I),j(J){} int sum(){return base::sum()+j;} }; void call(base b) { cout<

    N B S 3 Replies Last reply
    0
    • S simon wan

      its output is 47, why? #include "stdafx.h" #include class base { int i; public: base(base&){} base(int I=0):i(I){} virtual int sum(){return i;} }; class derived:public base { int j; public: derived(derived&){} derived(int I=0, int J=0):base(I),j(J){} int sum(){return base::sum()+j;} }; void call(base b) { cout<

      N Offline
      N Offline
      namaskaaram
      wrote on last edited by
      #2

      ur base class callz both the constructorz(when u mention one parameter)(i mean it callz the **base(base&)**thatz why..... :~ "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

      1 Reply Last reply
      0
      • S simon wan

        its output is 47, why? #include "stdafx.h" #include class base { int i; public: base(base&){} base(int I=0):i(I){} virtual int sum(){return i;} }; class derived:public base { int j; public: derived(derived&){} derived(int I=0, int J=0):base(I),j(J){} int sum(){return base::sum()+j;} }; void call(base b) { cout<

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, You wonder why the output is the same as the value j from your d Well the problem is this: You have a very huge error in your classes that messes up the stack pretty badly. Let me elaborate. You have 2 errors: first your copy constructors, second your function call. The first error is that your copy constructors do not return a class object. This alone doesn't cause the bug alone. The second error is that your functions call accepts a non reference object argument. This isn't a direct error (sometimes even no error), but in your program it is. This is because it takes more time to pass an object and most of the time, you don't want you object to be copied like that. These two errors lead to the following: when call() is invoked, the copy constructor of b is invoked. This constructor does not create a object and therefore the stack gets messed up. In the function call the variable b points to the wrong object on the stack (which is d). You can easy test this behaviour by changing the value of d.j, by removing the empty copy constructor or by passing b as a reference to call(). Hope this helps you to understand the problem. I also got the blogging virus..[^]

        1 Reply Last reply
        0
        • S simon wan

          its output is 47, why? #include "stdafx.h" #include class base { int i; public: base(base&){} base(int I=0):i(I){} virtual int sum(){return i;} }; class derived:public base { int j; public: derived(derived&){} derived(int I=0, int J=0):base(I),j(J){} int sum(){return base::sum()+j;} }; void call(base b) { cout<

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          It's because call() takes the paramater by value and not by reference. Because you've declared it to take the parameter by value, the passed object gets *sliced*. So even if you pass an instance of derived, it gets sliced and only the base part of the objects is passed to call. The sum function call then executes on the sliced base instance and so you get 47 and not 57 Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          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