DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Inheritance and pure virtual

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    Inheritance and pure virtual

    We had a base class that had a member function that incorrectly returned a pointer instead of a const pointer.
    While looking at this, I came across some behavior I didn't expect. The following code snippets illustrate.
    This code snippet fails to compile because Y is abstract:
    Code:
    #include <iostream>
    using namespace std;
    
    class X
    {
    public:
        virtual const char* getName() const=0;
    };
    
    class Y : public X
    {
    public:
        virtual char* getName() {
            static char name[] = "bob";
            return name;
        }
    };
    
    int main()
    {
        Y y;
        cout << y.getName() << endl;
        return 0;
    }
    That makes sense to me: "virtual const char* getName() const" is never defined.

    This fails to compile because of conflicting definition of the return type:
    Code:
    using namespace std;
    
    class X
    {
    public:
        virtual const char* getName() const=0;
    };
    
    class Y : public X
    {
    public:
        virtual char* getName() const {
            static char name[] = "bob";
            return name;
        }
    };
    
    int main()
    {
        Y y;
        cout << y.getName() << endl;
        return 0;
    }
    Why does the difference between the two (constness of the function) result in the two different errors?

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    overriding a function means that the overrider has to match exactly the overridden version. This means that the parameter list, the return type and the cv qualifications of the function itself, and its return type and parameter list must match. There's one exception to the rule: the return type can change from X* or X& to Y* or Y& in the derived class. Other than that, any mismatch is an error (effectively, a mismatch would be overloading, not overriding, except that you can't overload based on the return type). There are good reasons for this restriction: imagine a user deriving a class only to gain access to a vector's internal buffer for example. By removing the const from the return type, the derived class would enable hackers to tamper with sensitive data.
    BTW, it doesn't matter whether the function is virtual or pure virtual for that matter.
    Last edited by Danny; 04-28-2011 at 08:55 AM.
    Danny Kalev

  3. #3
    Join Date
    Mar 2011
    Location
    New York
    Posts
    5

    Inheritance and pure virtual

    I think that if you don't have any data in the base classes, you are not going to be hurt much by the diamond problem to begin with an then you don't even need to worry much about using virtual inheritance. Scope resolution for member functions would help resolve the ambiguities. But in cases where that base is again a derived of some class that has members that might cause problems. But then the advice doesn't seem to be considering that scope as well. If you have no state in the base class of the diamond it becomes a virtual diamond where not. For further information you may get reference from here http://www.thedegreeexperts.com

Similar Threads

  1. Inherited Forms Problem
    By kentuckyjoe in forum .NET
    Replies: 19
    Last Post: 01-09-2010, 10:21 PM
  2. Summary of This Discussion
    By Jeff Johnson in forum .NET
    Replies: 197
    Last Post: 07-23-2001, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center