Hi, guys. I have a problem with dynamic memory allocation and classes. I am to implement a class Folder that has methods to create, delete and print names of folders. The problem is that when call my AddFolder function and it allocates memory for a new folder, I don't seem to be able to access the newly created folder. The compiler doesnt show any error, but when I call printName(), my program stops working. Can you guys tell me what I'm doing wrong?
You are passing the pointer by value, so the modification doesn't get through to the main program. Try passing the pointer by reference. Also, the NULL check for the folder is completely useless ('new' throws on error rather than returning NULL), and when you have deleted something it is common practice to set the value of the pointer to NULL as well, unless you know its just about to go out of scope or something like that (but of course you don't in a member functions taking the pointer).