C++ Questions

Associate
Joined
20 Jul 2009
Posts
41
Just a few questions I have if people would be able to answer them for me :D?

(1) I have a static class called Config with private variables. This class is in a separate header/cpp file to keep everything tidy. I have made a function called initialise which sets the default values of these variables and it is called at the start of my main. The problem I am having is that when I am trying to access these private fields from initialise in the cpp, the compiler moans about a linking error. To solve this I am having to put something like "bool Config::_required;" at the top of my cpp. I just wondered if this was the proper way to solve this problem?

(2) When is a good time to use try/catch statements or when is a bad time?

(3) Edit: Removed

(4) I never seem to use destructors, when would these be used? I have only been making small applications as of yet.

(5) What are the important aspects of the stack and heap? Should I be coding in a way to utilize these? :S

I am using Windows7, Visual Studio 2008/2010. Thanks.
 
Last edited:
(1) You need to tell your linker to link that file in - in VS i think it's as simple as adding the file to the project

(2) Whenever there may be a fatal error that you can recover from - if you can't recover, you might as well crash.

(3) ...

(4) use a destructor to clean up any thing created by that class, also make sure you destroy dynamically (ie Class* variable = new Class()) otherwise you will leak memory

(5) stack and heap are different memory allocations, i forget which is which but basically one is where variables like int iNum is and the other is where int* iNum = new int() is.
 
(1) You need to tell your linker to link that file in - in VS i think it's as simple as adding the file to the project

(3) ...

(4) use a destructor to clean up any thing created by that class, also make sure you destroy dynamically (ie Class* variable = new Class()) otherwise you will leak memory

(1) I already have it added to my project. It only does this on my static class. I have other classes and they work as intended. Just the static class complained about linking errors so I presumed I had done something wrong.

(3) I was thinking about interfaces in C# sorry! I had a check list of things to research.

(4) Should I call a destructor when the application ends? Or just generally when I have finished with something?
 
On destructors, generally when you've finished with it, but ensure it's destroyed when your application ends.

In Embarcadero/Borland/Codegear C++ they have a tool called CodeGuard which will tell you all about leaks, overruns etc, i'm sure there must be a similar tool for VS
 
Never call a destructor yourself. They will get called automatically when the class instance goes out of scope

If the class instance is created dynamically with new, the destructor will run automatically when you call delete on the pointer.
 
In Embarcadero/Borland/Codegear C++ they have a tool called CodeGuard which will tell you all about leaks, overruns etc, i'm sure there must be a similar tool for VS

Thanks! I found an application called Visual Leak Detector, this works very well!

Never call a destructor yourself. They will get called automatically when the class instance goes out of scope

If the class instance is created dynamically with new, the destructor will run automatically when you call delete on the pointer.

Thanks, makes sense :)
 
Back
Top Bottom