who can tell me WHY?
-
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<
-
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<
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
-
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<
Hello, You wonder why the output is the same as the value
j
from yourd
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: whencall()
is invoked, the copy constructor ofb
is invoked. This constructor does not create a object and therefore the stack gets messed up. In the functioncall
the variable b points to the wrong object on the stack (which isd
). You can easy test this behaviour by changing the value ofd.j
, by removing the empty copy constructor or by passingb
as a reference tocall()
. Hope this helps you to understand the problem. I also got the blogging virus..[^] -
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<
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 thebase
part of the objects is passed to call. Thesum
function call then executes on the sliced base instance and so you get 47 and not 57 Regards Senthil _____________________________ My Blog | My Articles | WinMacro