I have made a preliminary demo using the anonymous field feature  
github.com/dontpanic92/swig

After wrapping your example C++ code, the following Go code acts the same 
as C++ code:

package main

import "test5"

func test(a test5.A) {
    a.M1()
}

func main() {
    b := test5.NewB()
    test(b)
}

It will print 

A::m1
B::m2


在 2016年10月4日星期二 UTC+8下午9:47:11,Ian Lance Taylor写道:
>
> On Tue, Oct 4, 2016 at 6:33 AM, Shengqiu Li <lisheng...@gmail.com 
> <javascript:>> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to