On Apr 5, 12:46 pm, nishit.gu...@st.com wrote:
************************************************** *********************
#include<iostream>
using namespace std;
class base {
public:
int i;
void func1(){
cout<<"\nsizeofbase="<<sizeof(*this)<<endl;
}
};
class derived : public base{
public:
int i, j, k;
void func2(){
cout<<"\nsizeofderived="<<sizeof(*this)<<endl;
}
};
int main(){
derived obj;
obj.func2();
obj.func1();
return 1;}
************************************************** ****************************
Output :
sizeofderived=16
sizeofbase=4
************************************************** ****************************
I think output shout be equal to 16 for both cases.
No.
Learn the difference between static type and dynamic type.
Dynamic type is only relevant in a very few, specially
designated cases: calling a member function *if* the static type
of the function declares it virtual, and in dynamic_cast and
typeid. In all other cases, the static type is used.
In the case of sizeof, this is particularly important, because
sizeof is defined as being an "integral constant expression";
that is, an expression that the compiler can evaluate, at
compile time, so that it can be used for things like template
arguments or array dimensions. If sizeof used the dynamic type,
then this wouldn't be the case. I suppose that the language
could also have provided a dynamic_sizeof, which used the
dynamic type (it wouldn't be hard to implement), but it doesn't.
Probably because the utility was seen as not being high enough.
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34