On Tue, Oct 4, 2016 at 6:33 AM, Shengqiu Li <[email protected]> wrote:
> Thanks for your reply. In my view, the difference seems not affect the
> behavior. As we store the pointer of the C++ object, when we call a C++
> virtual function in Go and the object itself is an instance of child class,
> calling the parent's function or the child's function are the same --
> finally they will call the child's function in C++. For example,

Here is an example showing the difference.

C++ code:

====================================================
#include <iostream>

class A {
 public:
  virtual void m1() { std::cout << "A::m1\n"; m2(); }
  virtual void m2() { std::cout << "A::m2\n"; }
};

class B : public A {
  virtual void m2() { std::cout << "B::m2\n"; }
};

int main() {
  B b;
  b.m1();
}
=====================================================

Go code:

=====================================================
package main

import "fmt"

type A struct{}

func (a A) m1() {
fmt.Println("A.m1")
a.m2()
}

func (a A) m2() {
fmt.Println("A.m2")
}

type B struct {
A
}

func (b B) m2() {
fmt.Println("B.m2")
}

func main() {
var b B
b.m1()
}
=================================================

The C++ program will print

A::m1
B::m2

The Go program will print

A.m1
A.m2

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to