virtual functions

Hi,

my question is:
Why does the first code give 1, 2, 3
but the second one 1, 1, 1?

1st code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <list>

class a
{
protected:
  int val;
public:
  virtual void increment(){val++;};
  void say(){std::cout << val << "\n";};
  a():val(0){};
};

class b : public a
{
public:
  void increment(){val+=2;};
  b(){};
};

class c : public a
{
protected:
  int val2;
public:
  void increment(){val+=3;};
  c():val2(0){};
};

int main ( int argc, char **argv )
{
  std::list<a*> l;
  a* aa = new a;
  b* bb = new b;
  c* cc = new c;
  l.push_back(aa);
  l.push_back(bb);
  l.push_back(cc);
  for(std::list<a*>::iterator c = l.begin(); c != l.end(); ++c)
  {
    (**c).increment();
    (**c).say();
  }
  return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <list>

class a
{
protected:
  int val;
public:
  virtual void increment(){val++;};
  void say(){std::cout << val << "\n";};
  a():val(0){};
};

class b : public a
{
public:
  void increment(){val+=2;};
  b(){};
};

class c : public a
{
protected:
  int val2;
public:
  void increment(){val+=3;};
  c():val2(0){};
};

int main ( int argc, char **argv )
{
  std::list<a> l;
  a aa;
  b bb;
  c cc;
  l.push_back(aa);
  l.push_back(bb);
  l.push_back(cc);
  for(std::list<a>::iterator c = l.begin(); c != l.end(); ++c)
  {
    (*c).increment();
    (*c).say();
  }
  return 0;
}
In the second code you are storing objects of type a in the list. The list can't store objects of type b or c. When you use push_back to add aa and cc the objects will be "sliced" and only the class a part of the objects are stored in the list. Slicing is almost always a bug.

a::increment() is called for all the elements because all of them are of type a.
In the second case the list can only hold elements of type a

So when derived elements are push back'ed onto the list they are sliced - i.e. the derived parts of the class removed.

The second list therefore only has elements of type a which have an increment of 1 (after being initialized to 0)

For the first list no slicing occurs
Last edited on
thanks Peter87 and mik2718 this explained. I asked this because no errors or warnings occurred. I marked it as solved.
Topic archived. No new replies allowed.