473,326 Members | 2,173 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,326 software developers and data experts.

size of class

have a look at the sample code
class myTest
{
private:
int i;
int d;
};
class myTest2:myTest
{
private:
int i;
int d;
};
int _main()
{
int s = sizeof(myTest2);
return 0;
}

-->why is s value will be 16.The private variables derivable.
can some body help regarding this.

Jun 14 '07 #1
5 1423

subkris...@gmail.com wrote:
have a look at the sample code
class myTest
{
private:
int i;
int d;
};
class myTest2:myTest
{
private:
int i;
int d;
};
int _main()
{
int s = sizeof(myTest2);
return 0;
}

-->why is s value will be 16.The private variables derivable.
can some body help regarding this.
Hi,

private is only an access specifier. When you derive from a class
privately the derived class size will be the total of base class size
and own class size. So the size of myTest+myTest2 will be the size of
myTest2.

So when you derive privately it does only mean that the base
class's private data or members cannot be accessed from derived class.
And it does not mean anything regarding the size.

Thanks and regards,
Amal P.

Jun 14 '07 #2
On 14 Juni, 13:25, subkris...@gmail.com wrote:
have a look at the sample code
class myTest
{
private:
int i;
int d;};

class myTest2:myTest
{
private:
int i;
int d;};

int _main()
{
int s = sizeof(myTest2);
return 0;

}

-->why is s value will be 16.The private variables derivable.
can some body help regarding this.
Sure, each int is 4 bytes on your system. You have 4 ints (2 in
myTest2 and 2 in myTest) and 4 * 4 = 16. Just because you can not
access myTest::i from myTest2 does not mean that they no longer exist.

--
Erik Wikström

Jun 14 '07 #3
On 14 Jun, 12:25, subkris...@gmail.com wrote:
have a look at the sample code
class myTest
{
private:
int i;
int d;};

class myTest2:myTest
{
private:
int i;
int d;};

int _main()
{
int s = sizeof(myTest2);
return 0;

}

-->why is s value will be 16.
if there is no padding and sizeof(int) == 4 than the
sizeof(myTest2) is 4*sizeof(int). there are two
ints in myTest and two in myTest2. private access
and private inheritance doesn't change that.
The private variables derivable.
I don't understand what you mean.

regards

DS

Jun 14 '07 #4
<su********@gmail.comwrote in message
news:11**********************@q19g2000prn.googlegr oups.com...
have a look at the sample code
class myTest
{
private:
int i;
int d;
};
class myTest2:myTest
{
private:
int i;
int d;
};
int _main()
{
int s = sizeof(myTest2);
return 0;
}

-->why is s value will be 16.The private variables derivable.
can some body help regarding this.
A little modification just to make it easier to show. The output of the
following program on my system is:
8 16
1 2 3 4

An instance of myTest2 holds 4 integers.

#include<iostream>

class myTest
{
public:
int i;
int d;
};

class myTest2: public myTest
{
public:
int i;
int d;
};

int main()
{
std::cout << sizeof( myTest ) << " " << sizeof( myTest2 ) << "\n";
myTest2 Foo;
Foo.i = 1;
Foo.d = 2;
Foo.myTest::i = 3;
Foo.myTest::d = 4;

std::cout << Foo.i << " " << Foo.d << " " << Foo.myTest::i << " " <<
Foo.myTest::d << "\n";
return 0;
}
Jun 15 '07 #5

Jim Langston <ta*******@rocketmail.comwrote in message...
>
A little modification just to make it easier to show. The output of the
following program on my system is:
8 16
1 2 3 4

An instance of myTest2 holds 4 integers.

#include<iostream>

class myTest{
public:
int i;
int d;
};

class myTest2: public myTest{
public:
OP: Once you understand Jim's example, add a 'char' here:

char c;

Normally this will cause 'padding' (as dasjotre mentioned) to be used.
So, instead of your class increasing by one 'byte', it may increase by
four ( 1 char + 3 pad bytes). [ depends on compiler, flags, and machine. ]
int i;
int d;
};

int main(){
std::cout << sizeof( myTest ) << " " << sizeof( myTest2 ) << "\n";
myTest2 Foo;
Foo.i = 1;
Foo.d = 2;
Foo.myTest::i = 3;
Foo.myTest::d = 4;

std::cout << Foo.i << " " << Foo.d << " " << Foo.myTest::i << " " <<
Foo.myTest::d << "\n";
return 0;
}
--
Bob R
POVrookie
Jun 15 '07 #6

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
3
by: ThazKool | last post by:
Is there anyway to write a class or struct that has no storage. It only operates on a reference to an already existing type. This is actually an extension to another thread. The thread went a...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
6
by: webinfinite | last post by:
Hi, How to find a STL class size, for example: string. I would like to know what the size of the implementation of this class, I am not interested in the size of its object but the class itself....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.