By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

size of class

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
<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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.