Thread: Have I found a black hole in C++?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Have I found a black hole in C++?

    Here some stuff which I did in Visual Studio 2005 and CodeWarrior.
    So, it's definitelly not a jsut compiler bug.

    I defined class like this:

    Code:
    class A
    {
    public:
    	virtual void B() = 0;
    };
    So, I have pure virtual method.
    Sure thing, we can't instantiate class.

    However, what I found I can add implementation for pure virtual method (which doesn't make any sense)

    Code:
    void A::B()
    {
    	printf("z");
    }
    And compiler won't say me a damn thing.
    I can't instantiate it still.

    However, I can do this:

    Code:
    class C: public A
    {
    public:
    	virtual void B();
    };
    
    void C::B()
    {
    	A::B();
    }
    First of all I was able to compile this, where I can pure abstract method.
    Second, I was able to debug it and step into A::B.

    It's definitelly crazy.

    Does anybody have explanation, why it works?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does anybody have explanation, why it works?
    Because it is allowed, and comes in handy when you want to have a pure virtual destructor since derived class destructors will invoke base class destructors even if the latter is pure virtual. Another use is to force concrete derived classes to override the virtual function, but provide a default implementation anyway that can be used in the override (as in your example, I notice).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Because it is allowed, and comes in handy when you want
    Got it. It look that I even found this in C++ specification.
    http://www.kuzbass.ru:8086/docs/isoc...class.abstract

    -8- A virtual function declared in a class shall be defined, or declared pure (class.abstract) in that class, or both ; but no diagnostic is required (basic.def.odr).

    to have a pure virtual destructor since derived class destructors will invoke base class destructors even if the latter is pure virtual.

    Another use is to force concrete derived classes to override the virtual function, but provide a default implementation anyway that can be used in the override (as in your example, I notice).
    Generally both cases are:
    1) We want derived classes to override something
    2) We still want to give some default implementation.

    I agree it looks this is a case.
    Last edited by vronin; 09-24-2008 at 04:06 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Because it is allowed, and comes in handy when you want to have a pure virtual destructor since derived class destructors will invoke base class destructors even if the latter is pure virtual. Another use is to force concrete derived classes to override the virtual function, but provide a default implementation anyway that can be used in the override (as in your example, I notice).
    The only thing, which I don't understand. Why it's done like that? I won't say that it's too handy...

    On one hand, if you want function to be overriden, you may define pure virtual and other helper function, which should be used in derived classes in overriden function.

    The main concern that when derived class will override such implemented virtual function compiler won't force developer to do a call of function from base class. So, anyway developer shouldn't forget to call function from base class.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Sometimes you are writing base classes as an interface that can be used to hide the inner workings of its derived classes. Given that fact, it is very handy. Especially when you may even be deriving your class from another class which has its member functions in a library or DLL where you cannot see its source. Thus you would need to explicitly call the virtual function of the parent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Puzzled.
    By silhoutte75 in forum C Programming
    Replies: 13
    Last Post: 01-21-2008, 05:17 PM
  2. arrays with elements
    By bradleyd in forum C Programming
    Replies: 5
    Last Post: 04-10-2007, 12:00 PM
  3. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM