Question with virtual functions
Asked Answered
W

4

7

I have two classes:

class x {
public:
  virtual void hello() {
    std::cout << "x" << std::endl;
  }
};

class y : public x {
public:
  void hello() {
    std::cout << "y" << std::endl;
  }
};

Can someone explain why the following two calls to hello() print different messages? Why don't they both print "y"? Is it because the first one is a copy while the second one actually points to the object in memory?

int main() {
  y  a;

  x b = a;
  b.hello(); // prints x

  x* c = &a;
  c->hello(); // prints y
  return 0;
}
Weinhardt answered 28/1, 2011 at 6:58 Comment(1)
I think this is the first time I've seen someone correctly guess the cause, good job.Liar
G
6

Yes, you are right

x b = a;

Invokes a copy constructor (b IS an 'x')

x& b = a;

Assigns a reference and will use the override (b is still actually a 'y')

Gibb answered 28/1, 2011 at 7:1 Comment(0)
P
6

Because x b = a; slices the object.

When this code executes, it creates a new x, not a y, which is a copy of the original y, a'.

Paraguay answered 28/1, 2011 at 7:2 Comment(0)
G
0

x b = a copies a to b. Since b is type x, you end up with an object of type x. An object of type x will print x.

The only way you get y is when you are calling into an object of type y.

Granular answered 28/1, 2011 at 7:2 Comment(0)
D
0

b.hello() prints "x" because b is an instance of class X. c->hello() prints "y" because c points to a, and a is an instance of class Y.

What might be confusing for you is that when you write x b = a;, you're creating a new object b and initializing it with a. When you write x* c = &a;, c is not a new object. You just introduced an alias to an existing object.

Drainage answered 28/1, 2011 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.