What I mean is that the difference doesn't affect the wrapper. Your example 
shows the situation of purely go code, but the wrapper code doesn't purely 
go code -- although the "path" are different, the destination is the same.

If your code acts as a wrapper -- let's say that A.m2 will call test_A_m2, 
the polymorphism on C++ side is still available. If we passed a pointer of 
an A object to test_A_m2, it will call A::m2; if we passed a pointer of B 
object to test_A_m2, it will call B::m2.



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

Reply via email to