is it right behaviour...if yes,why??

N

nishit.gupta

***********************************************************************
#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.
 
O

Ondra Holub

(e-mail address removed) napsal:
***********************************************************************
#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.

Yes, compiler is correct. For 2 independent reasons:
1. sizeof is evaluated during compilation - so sizeof(*this) in method
base::func1() is always equal to sizeof(base)
2. would you have special sizeof, which is evaluated in runtime, it
would be again equal to sizeof(base) when called in method
base::func1(). Inside of the method program does not know, that
intance is instance of derived. it sees only, that it is instance of
base.
 
I

Ian Collins

***********************************************************************
#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

Whitespace is free these days.
******************************************************************************
I think output shout be equal to 16 for both cases.
Why?
 
O

Ondra Holub

I think output shout be equal to 16 for both cases.

The number shown in output does not have to be 16 (it is platform and
alignment dependent), but it will not be definitely same for both
cases.
 
N

nishit.gupta

i agree with your first comment but for your comment, one more doubt,
******************************************************
Inside of the method program does not know, that
intance is instance of derived. it sees only, that it is instance of
*************************************************************
while calling a function this was thrown on the stack(which was of
derived class) and
the same this should(i am not sure) be used for sizeof....so how cud
be the result different???
 
N

nishit.gupta

Ian
i may be wrong but :
the this pointer thrown on stack was same (derived class objects's
address).
so while calculating sizeof it should use same this poinetr...please
clarify
 
I

Ian Collins

(e-mail address removed) wrote:

Please don't top post.
Ian
i may be wrong but :
the this pointer thrown on stack was same (derived class objects's
address).
so while calculating sizeof it should use same this poinetr...please
clarify

The result of sizeof is a compile time constant.

you may as well have written (with added whitespace!)

void func1(){
cout << " \n sizeof base = " << 4 << endl;
}
 
O

Ondra Holub

(e-mail address removed) napsal:
i agree with your first comment but for your comment, one more doubt,
******************************************************
Inside of the method program does not know, that
intance is instance of derived. it sees only, that it is instance of
*************************************************************
while calling a function this was thrown on the stack(which was of
derived class) and
the same this should(i am not sure) be used for sizeof....so how cud
be the result different???

this belongs always to a certain type. it is always a pointer to this
type. It does not matter, which derived class is the real instance. If
you call method of parent, you can access only methods of this parent
(and its parents), but no methods from derived classes. It is
abstraction. You can try following modification to your program:

#include <iostream>

class Base;
class Derived;

void Test(Base*)
{
std::cout << "Test(Base*)\n";
}

void Test(Derived*)
{
std::cout << "Test(Derived*)\n";
}

class Base
{
public:
Base() { }
void f1()
{
//std::cout << "f1: " << sizeof(*this) << '\n';
std::cout << "f1: ";
Test(this); // Test(Base*) will be called
}

int a;
};

class Derived: public Base
{
public:
Derived() { }

void f2()
{
//std::cout << "f2: " << sizeof(*this) << '\n';
std::cout << "f2: ";
Test(this); // Test(Derived*) will be called
}

int b;
int c;
int d;
};

int main()
{
Derived d;
d.f1();
d.f2();
}

You can see, that from method Base::f1 is always called function
Test(Base*). From method Derived::f2 is always called function
Test(Derived*).
 
S

Salt_Peter

***********************************************************************
#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.

That would break encapsulation. After the name lookup process func1()
knows nothing about the derived part of the object and neither does it
have any access to it. In essence, func1() can never access derived's
members using 'this', it can only access base's members.

The sizeof(derived) is not neccessarily 16 either. That depends on the
platform.
 
J

James Kanze

***********************************************************************
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top