new/delete in ctor/dtor

Started by
16 comments, last by MichaelBartman 20 years, 8 months ago
Thanks for all of your help guys,

I had tried doing temp = new b; but that it still had the same problem. That''s when I tried filling in a ctor and dtor for class b, btu that still didn''t work.

Deep copy just sounds more confusing than it really is . I had a quick look in my copy of C++ Primer 2nd Edition, but there wasn''t anything about it so I figured it was something complex :|

Thanks again for all of the help!
Michael BartmanCEO, Lead ProgrammerDark Omen Studios
Advertisement
In class a, define this:
a(const a&){    temp = new b;}a& operator =(const a& rhs){    if (this == &rhs) // not really necessary in this example, but it''s a good habit to always check for self-assignment        return *this;    delete temp;    temp = new b;    return *this;}

fallenang3l''s code also pointed out something else to watch for: If you''re implementing a copy constructor, you''ll also want to implement an assignment operator, because that''s another source of initialization/finalization. It''s the Rule of Three (which I seem to be quoting almost daily these days): If your class specifies a destructor, an assignment operator, or a copy constructor, it will most likely need all three.

How appropriate. You fight like a cow.
Is everyone blind? Did I not do that in my post? Indeed.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
MichaelBartman: There is absolutely nothing wrong with the code you posted. The problem must be in the code you omitted or somewhere else.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
I don''t know how it could be any code that I omitted, because there is only a problem when I do a new on an object of type b, and then delete it. I don''t do anything to it besides that, and when I comment out the line with the new and the line with the delete everything works fine and dandy.

Maybe Visual Studio is just telling me I should have stuck with 6 and not gone to .NET? lol
Michael BartmanCEO, Lead ProgrammerDark Omen Studios
quote:Original post by MichaelBartman
Maybe Visual Studio is just telling me I should have stuck with 6 and not gone to .NET? lol


...hardly. Implement what the other posters have already mentioned and you should be fine. If defining a proper copy ctor, dtor, and assignment operator doesn''t fix the problem, then your problem lies elsewhere.
--Michael Fawcett
quote:Original post by C-Junkie
quote:Original post by dmounty
why would this only happen with a release compile, and not with a debug compile?


Because the release compile optimizes something which exposes the bug in a way where its not just an unchecked error code someplace, but a crash bug...

Never happened to you before? Lucky you...

Its happened to me and the ensuing language spewing from my mouth made the wallpaper peel.

This topic is closed to new replies.

Advertisement