Click here to Skip to main content
15,921,113 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Inheritance Pin
William Burton23-Apr-06 7:19
William Burton23-Apr-06 7:19 
AnswerRe: Inheritance Pin
John R. Shaw23-Apr-06 11:54
John R. Shaw23-Apr-06 11:54 
QuestionRe: Inheritance Pin
William Burton23-Apr-06 12:17
William Burton23-Apr-06 12:17 
AnswerRe: Inheritance Pin
Stephen Hewitt23-Apr-06 13:46
Stephen Hewitt23-Apr-06 13:46 
QuestionRe: Inheritance Pin
William Burton23-Apr-06 15:16
William Burton23-Apr-06 15:16 
AnswerRe: Inheritance Pin
Stephen Hewitt23-Apr-06 16:09
Stephen Hewitt23-Apr-06 16:09 
QuestionRe: Inheritance Pin
William Burton24-Apr-06 0:18
William Burton24-Apr-06 0:18 
AnswerRe: Inheritance Pin
Stephen Hewitt24-Apr-06 1:38
Stephen Hewitt24-Apr-06 1:38 
I can see a number of problems here. One of them is the following usage pattern:
{
    SSK temp; // Make an object on the stack.
    list.addNode(&temp); // Add a pointer to the temporary object to the list.
}
// When we get here the local "temp" no longer exists as it has gone out of scope.
// But we placed a pointer to it in "list": this means we have a pointer in "list"
// to an object that doesn't exist.


To fix this problem use code like this:
SSK pTemp = new SSK; // Create an object on the heap.
list.addNode(pTemp); // Add a pointer to the object to the list.


Now the pointer in "list" doesn't outlive the object it points to (when it does the pointer is known as a "dangling pointer").

You have to remember to delete the object you newed when you no longer need it: with new you manage the lifetime of the object explicitly. For example to empty "list" and delete all the objects it contained you can use code like this:
// Helper class - Put it in some .h file somewhere.
template <typename T>
struct Delete_Object
{
    bool operator()(T *pObj) const
    {
       delete pObj;
       return true;
        // Just returning a value for MSVC6 (it's got a bug which forces us to).
    }
};
 
// With this helper class we can delete all the contents of "list" and empty it like this
// (assuming you're using STL for "list". I don't think you are because of the function <code>addNode</code>.
// You'll have to adapt this code).
for_each(list.begin(), list.end(), Delete_Object<Vessel>());
list.clear();


Finally because we're calling delete on the base class but the actual object is of a derived type the descructor has to be virtual:
class Vessel
{
public:
    virtual ~Vessel()
    {
       // Clean stuff up here if you need to.
    }
};


Steve
GeneralRe: Inheritance Pin
David Crow24-Apr-06 2:59
David Crow24-Apr-06 2:59 
GeneralRe: Inheritance Pin
John R. Shaw29-Apr-06 20:29
John R. Shaw29-Apr-06 20:29 
GeneralRe: Inheritance Pin
David Crow1-May-06 2:51
David Crow1-May-06 2:51 
QuestionPrinter Font Question Pin
JKJKJK23-Apr-06 4:53
JKJKJK23-Apr-06 4:53 
AnswerRe: Printer Font Question Pin
Blake Miller26-Apr-06 7:46
Blake Miller26-Apr-06 7:46 
Questionhow can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
lekshmipriya23-Apr-06 2:49
lekshmipriya23-Apr-06 2:49 
AnswerRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
YaronNir23-Apr-06 3:33
YaronNir23-Apr-06 3:33 
QuestionRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
lekshmipriya23-Apr-06 4:00
lekshmipriya23-Apr-06 4:00 
AnswerRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
YaronNir23-Apr-06 4:09
YaronNir23-Apr-06 4:09 
QuestionRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
lekshmipriya23-Apr-06 5:55
lekshmipriya23-Apr-06 5:55 
AnswerRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
YaronNir23-Apr-06 6:01
YaronNir23-Apr-06 6:01 
AnswerRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
Trollslayer23-Apr-06 6:05
mentorTrollslayer23-Apr-06 6:05 
QuestionRe: how can i pass a password to a shellexecute() to extract a zip file from my VC++6.0 program Pin
David Crow24-Apr-06 3:04
David Crow24-Apr-06 3:04 
QuestionList Box have a error ??? Pin
Surivevoli23-Apr-06 1:32
Surivevoli23-Apr-06 1:32 
AnswerRe: List Box have a error ??? Pin
YaronNir23-Apr-06 2:00
YaronNir23-Apr-06 2:00 
GeneralRe: List Box have a error ??? Pin
Surivevoli23-Apr-06 19:20
Surivevoli23-Apr-06 19:20 
QuestionAbout the Bitmap Transparency Pin
onlysaint23-Apr-06 1:09
onlysaint23-Apr-06 1:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.