about the size of a derived class

1
2
3
4
5
6
7
8
9
10
class Base
{
  int i;
}

class Derived:public Base
{
}

Derived d;


we know that
d.Base::i
is
different from
d.Derived::i
so
it seems that there are two "i"s
why the size of Derived(or its object)is still 4?
There's only one 'i' here: Base::i.

Derived inherits Base::i. Both classes are using the same variable.

Base::i and Derived::i are only different if each class delcares there own variable. Which isn't the case here.
Last edited on
Base::i and Derived::i are only different if each class delcares there own variable. Which isn't the case here.
-----------------------------------------------------
Yes, I was considering this case
so in this case, what is the size of Derived(or its object)
if it is still 4, why are there two different values?
I'm not sure I understand your questions.

The size of a class has a size of the sum of all its members (at least -- it's possible for the size to be even larger than that due to other things, like vtables, padding, etc).

In the case of inheritance, child classes have everything their parents have (it's all inherited).

So in this case:

1
2
3
4
5
6
7
class Parent
{
  int i;
};

class Child : public Parent
{};


Here, Parent has a size of at least sizeof(int) (which is often 4)
Child also has a size of at least sizeof(int), since it contains 1 int (Parent::i)


If we change that to something like the below:
1
2
3
4
5
6
7
8
9
class Parent
{
  int i;
};

class Child : public Parent
{
  int j;
};


Now, Parent contains 1 int (i), and Child contains two ints (i, j).

so sizeof(Parent) >= sizeof(int)
and sizeof(Child) >= (2 * sizeof(int))
I meant there are two "i"s
one is Child::i
and the other is Parent::i
if this is not the case, why Child::i and Parent::i are sometimes different?
I meant there are two "i"s
one is Child::i
and the other is Parent::i


This is not true.

why Child::i and Parent::i are sometimes different?


They're only different if you give Child and Parent an 'i'.

1
2
3
4
5
6
7
8
9
10
class Parent
{
  int i;
};

class Child : public Parent
{};

// Only one i:  Parent::i
//  Child::i uses Parent::i, so they're the same variable 


1
2
3
4
5
6
7
8
9
10
11
12
class Parent
{
  int i;
};

class Child : public Parent
{
  int i;
};

// now we have 2 i's.  One is Parent::i and one is Child::i
//  here, they are 2 different variables. 
Last edited on
closed account (1vRz3TCk)
Maybe a diagram would help?

If you can view class Base as:
+----------+
|Base      |
+----------+
| int i    |
|          |
+----------+


then conceptualy speaking class Derived:public Base would be:
+--------------+
|Derived       |
+--------------+
| +----------+ |
| |Base      | |
| +----------+ |
| | int i    | |
| |          | |
| +----------+ |
|              |
+--------------+


An object of type Derived would only have the one i that is from the 'enclosed' Base object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Base
{
public:
    int i;
};

class Derived :public Base
{

};

int main()
{
    Derived d;

    d.i = 1;          //they all address the same i
    d.Base::i = 3;    //
    d.Derived::i = 4;

    std::cout << d.i << std::endl;

    return 0;
}



For an extreme example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Base1
{
public:
    int i;
};

class Base2
{
public:
    int i;
};

class Derived :public Base1, public Base2
{
public:
    int i;
};

the conceptual view would be:
+--------------+
|Derived       |
+--------------+
| +----------+ |
| |Base1     | |
| +----------+ |
| | int i    | |
| |          | |
| +----------+ |
|              |
| +----------+ |
| |Base2     | |
| +----------+ |
| | int i    | |
| |          | |
| +----------+ |
|              |
|int i         |
+--------------+

Here there are three is, you need to use scope resolution here to know which i you are referring to
d.Base1::i for the first
d.Base2::i for the second
and
d.i or d.Derived::i for the third
Last edited on
Agree to CodeMokey!
The class of drived has a pointer to the Base::i, so, it has the size!
Last edited on
Topic archived. No new replies allowed.