Advertisement

Virtual?

Started by January 19, 2003 05:42 PM
56 comments, last by John Dowis 21 years, 9 months ago
If the Standard says that something will produce undefined results, then I''d like to have an idea of why that is the case. The reason I''ve been told is that the first destructor called when destroying an object with delete is resolved based on the dynamic type of the object. That requires a virtual function. I doubt many people are satisfied when they are just told that declaring a destructor non virtual when it is required will result in undefined behaviour. I''d certainly perfer a vague understanding of why.

But I can now see that whilst you are programming, you should not be trying to think about this in terms of the reasons why the behaviour is undefined, you should just be thinking that the behaviour is undefined.
"C combines all the power of assembly language with all the ease of use of assembly language"
quote: Original post by NotAnAnonymousPoster
If the Standard says that something will produce undefined results, then I''d like to have an idea of why that is the case.

Usually because defining the behaviour would force some implementations to utilise inefficient mechanisms for realising the behaviour. C++ sacrifices all manner of sanity at the altar of performance.
quote:
The reason I''ve been told is that the first destructor called when destroying an object with delete is resolved based on the dynamic type of the object.

It''s got nothing to do with what might or might not result. The reason it''s undefined is that defining the behaviour then forces derived classes to carry additional information to determine how all subparts can be deleted in polymorphic fashion. For programs where you have a derived which will *never* be deleted in a polymorphic fashion, this violates the rule "you don''t pay for what you don''t need". It''s yet another borderline case which results in so many books appearing on the market about how to make C++ behave in anything like a sane manner.
quote:
But I can now see that whilst you are programming, you should not be trying to think about this in terms of the reasons why the behaviour is undefined, you should just be thinking that the behaviour is undefined.

Right. And you have to have a reason to go with undefined behaviour (there are reasons, such as when an implementation provides well-defined behaviour, but relying on UB is always non-portable).
Advertisement
quote:
Thinking for yourself does not involve rejecting what other people say out of hand. It involves considering what other people say and, if you feel a claim should be rejected, coming up with reasoning for why it should be rejected

I did consider what people said - I did my research and then I tested in with my compiler. Then fruny explained to me even though that is what may happen it is not to the standard. I realized I was wrong, Sark made the comment that 3 people disagree with me so I better give up. I was not trying to debate the standard I was meerly addressing the issue that people shouldn''t give up in an arguement because of the numbers on one''s oppenents size.

I agree with NotanAnonymous poster. The standard is very vague and I also had liked to know the reason why it was an undefined behavior. I''m happy you took the time to explain to us all why it is undefined.
quote: Original post by ph33r
I was meerly addressing the issue that people shouldn''t give up in an arguement because of the numbers on one''s oppenents size.

I have to agree with that. I''d have given up long ago if I was worried about weight of numbers.
Are you saying that the Standard chooses to declare undefined behaviour as opposed to outright enforcing objects to carry all the necessary information around with them for how to delete themselves, thereby getting rid of the idea of virtual destructors altogether? I agree with what you are saying (if I have understood it correctly) and admit that my previous post was not explaining what I thought it was. But I still think it''s worthwhile knowing what virtual destructors actually mean. Since you are given the choice between virtual and non virtual destructors, it is important to realise why an object being deleted polymorphically only makes sense if its destructor is declared virtual. And to my mind, that is because the first destructor which is called is the one belonging to the dynamic type of the object, which requires the behaviour of virtual functions.
"C combines all the power of assembly language with all the ease of use of assembly language"
quote: Original post by SabreMan
It''s yet another borderline case which results in so many books appearing on the market about how to make C++ behave in anything like a sane manner.

I keep getting the impression that you don''t really like C++ very much. You''ve given plenty of reasons of where you believe the language is flawed, but just out of curiosity, do you ever have anything nice to say about it?
"C combines all the power of assembly language with all the ease of use of assembly language"
Advertisement
quote: Original post by NotAnAnonymousPoster
Are you saying that the Standard chooses to declare undefined behaviour as opposed to outright enforcing objects to carry all the necessary information around with them for how to delete themselves

Yes. As I say, C++ introduces all manner of oddities in the name of efficiency.
quote:
Since you are given the choice between virtual and non virtual destructors, it is important to realise why an object being deleted polymorphically only makes sense if its destructor is declared virtual.

Yes, but you cannot use the same rationale as you would for normal virtual functions, since a dtor is not the same as a normal function. The rationale, therefore, becomes "if you are to delete a derived via a pointer to base, the base dtor must be virtual else the behaviour is undefined".
quote: Original post by NotAnAnonymousPoster
I keep getting the impression that you don''t really like C++ very much.

Whatever gives you that impression?
quote:
You''ve given plenty of reasons of where you believe the language is flawed, but just out of curiosity, do you ever have anything nice to say about it?

I''ve spent over ten years programming with C++, I''ve worked with millions of lines of C++ code (and even written a few), I''ve made a huge intellectual investment in learning the dark corners, I''ve read almost every acclaimed book, I''m fairly well known in the C++ community, I''m a member of ACCU, I''ve met a few members of the C++ committee (including Stroustrup), and I''d like to think I have some clue of where I''m coming from. In short, I have quite a lot to lose from shunning C++. So, why do you think I would still do that?
quote: Original post by SabreMan
Yes, but you cannot use the same rationale as you would for normal virtual functions, since a dtor is not the same as a normal function.

But there are similarities. The virtual keyword allows something to be treated polymorphically, so that''s what you need when you want to delete an object polymorphically. Even if it''s only a half truth, it still provides a bit of sense to the situation.
quote: Original post by SabreMan
I''ve spent over ten years programming with C++, I''ve worked with millions of lines of C++ code (and even written a few), I''ve made a huge intellectual investment in learning the dark corners, I''ve read almost every acclaimed book, I''m fairly well known in the C++ community, I''m a member of ACCU, I''ve met a few members of the C++ committee (including Stroustrup), and I''d like to think I have some clue of where I''m coming from.

I never implied that you don''t.
quote: Original post by SabreMan
In short, I have quite a lot to lose from shunning C++. So, why do you think I would still do that?

Because you believe that C++ is an inferior programming language. You also clearly care more about finding the right answer than feeling that your intellectual investment has truly been worth it. Then again, perhaps you just like to be controversial. Anyway, I was just wondering if you felt that there was any part of C++ which made it stand out in a positive way. And since I have not made a significant investment in learning C++ I''m not too bothered if you tell me that you don''t think it has any real redeeming feature.
"C combines all the power of assembly language with all the ease of use of assembly language"
quote: Original post by NotAnAnonymousPoster
I never implied that you don't.

That was a pre-emptive defence of anyone who might jump in and claim I reject C++ because I'm too stupid to learn it. Various people on these forums are idiotic enough to use that sort of tactic.
quote:
Anyway, I was just wondering if you felt that there was any part of C++ which made it stand out in a positive way.

I've earned a reasonable amount of money from knowing stupid stuff about it. Does that count?
quote:
Because you believe that C++ is an inferior programming language.

Right, but I didn't used to think that, so something caused a change.

[edited by - SabreMan on January 22, 2003 11:55:43 AM]

This topic is closed to new replies.

Advertisement