CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    38

    polymorphism in simple classes

    In this thread I have proved that

    PHP Code:
    class A
    {
    public:
        static 
    void func(){std::cout<<"A\n";}
    };

    class 
    B:public A
    {
    public:
        
    virtual void func(){A::func();}
    };

    class 
    C:public B
    {
    public:    
        
    virtual void funx(){func();}
    };

    class 
    D:public C
    {
    public:
        
    virtual void func(){std::cout<<"CC\n";}
        
    void Call(){funx();}
    };

    int _tmain(int argc_TCHARargv[])
    {
        
    D b;
        
    b.Call();
        return 
    0;

    the D::func is not polymophically called in the client code and in C++ I can eliminate "virtual" key, as it is indifferent, but in managed C++
    PHP Code:
    ref class A
    {
    public:
        static 
    void func(){Console::WriteLine("A");}
    };

    ref class B:public A
    {
    public:
        
    virtual void func(){A::func();}
    };

    ref class C:public B
    {
    public:    
        
    virtual void funx(){func();}
    };

    ref class D:public C
    {
    public:
        
    virtual void func() override {Console::WriteLine("CC");}
        
    void Call(){funx();}
    }; 
    I can't eliminate the virtual key in the D class. I wonder is this D::func polymorphically created once called ? Thank you
    Last edited by terminalXXX; June 20th, 2013 at 10:22 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: polymorphism in simple classes

    The fact that, unlike ANSI C++, the virtual keyword is not optional in C++/CLI when overriding a virtual base class member fuction (as well as the necessity of the override keyword in this context, that you already have recognized), simply is one of the syntactic differences between the two languages. AFAICT at least part of the reason why this requirement was introduced is enforcing code clarity: When you override a virtual base class function in ANSI C++ and omit the virtual keyword since it's implied anyway, readers of your code may miss the fact that it's a virtual function entirely, unless they also look at the base class declaration.

    Also, you can do some rather funky things with virtual funtions in C++/CLI that are not (at least not the same way) possible in ANSI C++. For more info about this see http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx and http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx.

    Quote Originally Posted by terminalXXX View Post
    I wonder is this D::func polymorphically created once called ? Thank you
    I have no idea what you mean by this, though.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured