473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Apr 5 '07 #1
9 1174

ni**********@st.com 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.

Apr 5 '07 #2
ni**********@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
Whitespace is free these days.
************************************************** ****************************
I think output shout be equal to 16 for both cases.
Why?

--
Ian Collins.
Apr 5 '07 #3
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.

Apr 5 '07 #4
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???
base.
Ondra Holub wrote:
ni**********@st.com 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.
Apr 5 '07 #5
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

Ian Collins wrote:
ni**********@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

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

--
Ian Collins.
Apr 5 '07 #6
ni**********@st.com wrote:

Please don't top post.
>
Ian Collins wrote:
>>>

Why?
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;
}
--
Ian Collins.
Apr 5 '07 #7
ni**********@st.com 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*).

Apr 5 '07 #8
On Apr 5, 6:46 am, 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.
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.
Apr 5 '07 #9
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

Apr 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ted Mencini | last post by:
When I replaced older <IMG ALIGN=RIGHT ...> tags with a CSS definition <IMG class=right ...> xxx.css: IMG.right { BORDER: 0; align: right } I notice that the effect is NOT the same....
2
by: Joona I Palaste | last post by:
AFAIK the C standard divides behaviour of different things into four classes. 1) Defined behaviour. The implementation must do exactly what the standard says. 2) Implementation-defined behaviour....
12
by: DeepaK K C | last post by:
main() { int a =100; a = a>>32; printf(" %d", a); } it prints "a" as 100 only....but I am expecting a = 0..... can some one tell me the reason?
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
3
by: vijaynats | last post by:
I have a treeview with a ContextMenu attached. When i click on a node, AfterSelect fires but does not fire when right clicked (the context menu pops up). (Background: I have loaded a list of...
1
by: Greg | last post by:
When a datagrids RightToLeft property is set to true, as expected, it right aligns the caption text, and reverses the order of the columns. When typing text into a cell, the text is right aligned....
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
2
by: johkar | last post by:
My right column content is created dynamically by an application so it will vary in height. When the main content of the page takes up more vertical space than the rigth column, I want the content...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
13
by: Roderik | last post by:
Hi, I am wondering why (half of the) squared images are not aligned on the right of the text in Internet Explorer (using <img ... align="right" />. I thought this was supported even in IE. In FF...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.