-5

I can't express my question in words. Please look the code below, I hope you will understand my question.

I have a class and an interface as shown below.

class MyInterface
{
public:
    virtual ~MyInterface(){}
    virtual void print() = 0;
};

class MyClass : public MyInterface
{
public:
    MyClass(){}
    ~MyClass(){}
    void print()
    {
       printf("Hello World\n");
    }
};

Now here's my question.

MyClass* myclass = new MyClass();
myclass->print(); //will print "Hello World"
MyInterface* pMyInterface = (MyInterface*)myclass;
pMyInterface->print();

Will the second call print Hello World as well? If yes, then why?

Baba
  • 47
  • 8
ceptonite
  • 21
  • 1
  • 6
  • 6
    Have you tried it? Did you encounter any problem? – songyuanyao Apr 28 '16 at 10:08
  • No, not yet. I have a project going on. – ceptonite Apr 28 '16 at 10:10
  • 3
    Seems like a pretty quick thing to try.... – Joe Apr 28 '16 at 10:10
  • @songyuanyao "Have you tried it?" is bad advice in a language full of undefined behavior, implementation-defined behavior and other pitfalls, such as C++. –  Apr 28 '16 at 10:10
  • 3
    @Zoidberg Can't agree that, especially for such a simple and fundamental issue. – songyuanyao Apr 28 '16 at 10:11
  • @songyuanyao similar simple questions include "does `int* x; return 1 + *x;` work?" –  Apr 28 '16 at 10:12
  • 2
    @Zoidberg The question should have at least went "I tried it and got result X. Does the standard guarantee this result?" – Rotem Apr 28 '16 at 10:12
  • @ceptonite What do you actually think is the purpose of an interface, if that wouldn't work? – πάντα ῥεῖ Apr 28 '16 at 10:12
  • @ceptonite You might want to see [Why do we need Virtual Methods in C++?](http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c) for more informations. – songyuanyao Apr 28 '16 at 10:16
  • @ceptonite: just FYI /- you can post code into online compilers such as http://ideone.com - it takes less time than creating a question here. Rotem's right that it would have been worth trying this even if you wanted confirmation of the behavioural guarantees. – Tony Delroy Apr 28 '16 at 10:18
  • It worked thanks Tony D. – ceptonite Apr 28 '16 at 10:21
  • @TonyD But I don't know why it worked. – ceptonite Apr 28 '16 at 10:22
  • @ceptonite: it works because that's what virtual functions do: you can call through to the derived-class implementation using a pointer of the base class type (or a reference). If you didn't understand the idea, how did you write the code? Or did you stumble across it somewhere? Or do you understand the behaviour but not understand how the implementation orchestrates it (i.e. virtual dispatch tables)? What do you want to know about? – Tony Delroy Apr 28 '16 at 10:38
  • I understood now, I'd just gone through the link which Maxim provided. – ceptonite Apr 28 '16 at 10:43

1 Answers1

0

One note is that you do not need to explicitly cast to an accessible base class, like you do in MyInterface* pMyInterface = (MyInterface*)myclass;. It is an implicit conversion from a pointer/reference to a derived class to that of an accessible base class.

In fact, such casting may introduce bugs if the classes are unrelated.

Find more details in virtual function specifier.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • @ceptonite I suggest you read that article about `virtual` functions. It is pretty fundamental thing, there is no point wasting ink on it because it is well documented in numerous textbooks and references. – Maxim Egorushkin Apr 28 '16 at 10:19