Thread: Is there any hidden complication?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manav View Post
    Anyway, I will use virtual instead of using pure virtual which, requires more work in every derived class.
    Not really since each of them should have a virtual destructor anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Do you mean that, if I have, virtual ~Base(), I should (Or, must?) make virtual ~all-otherDerived(), also?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, otherwise they will also be abstract.
    You must override each and every pure virtual function in the derived classes. Any pure virtual functions will make the class abstract.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I am confused, this worked fine without adding a virtual ~Derived2():
    Code:
    #include <iostream>
    using namespace std;
    
    class Base2 {
    public: virtual ~Base2() {
    		cout << "~Base2()" << endl;
    	}
    };
    
    class Derived2 : public Base2 {
    public: ~Derived2() {
    		cout << "~Derived2()" << endl;
    	}
    };
    
    int main() {
    	Base2* basePtr = new Base2;
    	delete basePtr;
    	basePtr = new Derived2;
    	delete basePtr;
    	return 0;
    }
    Output:
    ~Base2()
    ~Derived2()
    ~Base2()

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is another issue. You don't need to add the virtual keyword to subsequent functions in derived classes if the function is virtual in the base. They will be virtual anyway.
    But for clarity, I always add virtual to all functions.
    Try making the Base destructor pure virtual and omitting the one in Derived. The code won't compile if you create a Derived (or shouldn't).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Try making the Base destructor pure virtual and omitting the one in Derived. The code won't compile if you create a Derived (or shouldn't).
    It should and would. The implicitly created destructor is a valid override of the pure function.

    The code wouldn't link, though, because you still need an implementation of Base::~Base, even if you declared it pure.
    Code:
    inline Base::~Base() {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    It should and would. The implicitly created destructor is a valid override of the pure function.
    I see, I see. Guess that's what I get for never trying out a virtual destructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by CornedBee View Post
    It should and would. The implicitly created destructor is a valid override of the pure function.
    That seems strange but magical also, anyway, less work on programmer's part.

    The code wouldn't link, though, because you still need an implementation of Base::~Base, even if you declared it pure.
    Code:
    inline Base::~Base() {}
    That is unclear to me, besides, testing that it links or not, I am unable to explain why it is happening? And what is inline doing here?

  9. #24
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That seems strange but magical also
    Why? The compiler generates a destructor - it has to. Why should this destructor be any different from an explicitly defined constructor in its ability to override the base version?

    As for the link issue, I'll try to explain.
    Code:
    struct Base
    {
      virtual ~Base() = 0;
    };
    
    struct Derived : Base
    {
    };
    
    int main()
    {
      Derived d;
    }
    This is the basic situation. Now the compiler generates a destructor for Derived. Including the implicit call to the base, the final version of a destructor looks like this:
    Code:
    Derived::~Derived()
    {
      // Put code from the user-defined destructor here.
      // Put calls to members' destructors here.
      // Put calls to base destructors here.
      Base::~Base();
      // Many compilers end with this:
      return this;
    }
    See that call to Base::~Base()? It must be there, because the base class must be destructed, no matter what. So the call is generated. The linker then looks for Base::~Base(), but it won't find it, because there is none. The explicit declaration in Base means that the destructor isn't generated, but you don't define it either.

    So you have to define it explicitly. Since Base has nothing special to do, it's empty. It still has to be there.
    Code:
    Base::~Base() {}
    As for the inline, it allows the compiler to inline non-virtual calls to the destructor. And because all calls to the destructor are non-virtual (virtual calls aren't possible due to it being pure), that's a good idea. It means the compiler can then optimize the destructor away completely.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I must have missed it, but why are you storing a pointer to Base in Derived?

  11. #26
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks CornedBee.

    Elks, Do you remember the other situation in different thread?
    Array of something is-a something. Similar situation here.
    I was just looking for ways to get a cosmic type. I didn't know what is cosmic about it. But laserlight has told this to me.

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    So now a Derived HAS-A and IS-A Base? I think I'd roll my own code and stop using generated cruft like that. Or maybe use a language with more elegant support for calling web services (maybe Java, perl, python?).
    Last edited by medievalelks; 05-30-2008 at 08:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hidden the information --Help!! (T.T)
    By nghoanglinh223 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2008, 09:07 AM
  2. Make a file hidden
    By manutd in forum C++ Programming
    Replies: 19
    Last Post: 11-05-2006, 11:27 AM
  3. Showing hidden files
    By hunterdude in forum Windows Programming
    Replies: 8
    Last Post: 06-10-2005, 11:02 AM
  4. can the procedure in the program be hidden?
    By Jasonymk in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2003, 04:44 PM
  5. html form - writing ip to a hidden field
    By iain in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-16-2001, 03:29 AM