I noticed that in Visual Studio 2013 that when one creates a new class, it assigns a file per class. I am accustomed to programming in C++ where I would put the main class with some helper classes.
I am putting together some basic demonstration code, and I want to avoid risky practices like dynamic memory allocation with the possibility of leaks from a missing Delete from New issue. So I was thinking of only keeping the dynamic memory allocation within the class constructor or destructor. Thus I would have a number of helper classes for different sorts of objects needed by the main class, in the same file as the main class. When I wrote C++ previously, I could keep these helper classes in the same file, but under Visual Studio I am getting the error message that these classes are not recognized!
I am leaning towards the approach of creating a static library project. But it seems like a bit of an over-kill. The idea of the .cpp file was to be a logical unit of code in one place before.
What is the common way of accomplishing this? I could use vector allocation, but I want a clean way of passing the information back and forth. I was looking as a in-between of creating a memory allocation and de-allocation class for each of the types of objects that need memory allocation. Before I would just keep the memory allocation in the cpp file?
1) Is there an option in Visual Studio 2013 to allow multiple classes in the same cpp file?
2) For the workaround memory class file creation, if one creates two types both based on integer, say "typedef int Type1; and typedef int Type2;" is this sufficient to create two constructors that override based on the type? I want to be able to create units of different kinds with the same initialization.
3) Right now I am trying the workaround of creating separate structures for each data type.
Any suggestions would be appreciated. Thank you!
Updated information in response to Solution #1:
I have a class file CDCDirectControls that has the class that I am writing.
From CDCDirectControls.h, the class for the file name and nested class is declared but not defined (using the guideline that in the header file declares and the .cpp file is used to define) :
Nested class definition - the class is nested to "cleanly" override the constructors, to provide the facility for different controls:
class CCDCDirectControls : private CView
{
public:
CCDCDirectControls();
~CCDCDirectControls();
class Button;
CCDCDirectControls(CCDCDirectControls_Button x1);
}
Now in the CCDCDirectControls.cpp file, first is defined the nested class. Only after the nested class is defined, then is the constructor defined that is based on the nested class. Visual Studio 2013 seems to complain if the object is instantiated before it is defined with an error message.
class CCDCDirectControls::Button { public:
int x_anchor; int y_anchor;
};
CCDCDirectControls::CCDCDirectControls(CCDCDirectControls::Button x1) {
}
Finally, the call to the constructor was made like:
CCDCDirectControls cdc1;
CCDCDirectControls cdc(CCDCDirectControls::Button x1);
I am going to work with this for a while and see how it goes. This seems like the cleanest approach to do this sort of thing. The Button nested-class can be used to define what it is. And then the constructor can be used to add the button on to the window. I was also able to compile with CCDCDirectControls as a static class also, but I had to make sure that the static class was of global scope (it seems a shame to just have to copy and comment the code inside the procedure itself).
Is there any cleaner way of doing this?
I found a reference to nested classes here:
nested classes - cppreference.com[
1]
I found a reference to implementation of nested classes here:
Nested class' function: implementation c - C++ Forum[
2]
I found a reference to the C2267 compiler error (when a static class is defined without global scope here)
Compiler Error C2267[
3]
I worked around it using anonymous name space as here (and it compiled without error):
namespace
{
static CCDCDirectControls cdc3(CCDCDirectControls::Button x1);
}
A reference to usage of the anonymous name space is here: it is supposed to keep the static variable from being referenced outside of the .cpp file:
C++ initialize static variables in class? - Stack Overflow[
4]
Updated:
In response to Solution #2 (and I appreciated the feedback), I looked up "std::unique_ptr"; now I have a bunch of code that references an existing array. The problem I might have with std::unique_ptr is the reference to it, but that seems covered by reference 5. It seems that unique_ptr might be the best way to dynamically allocate the memory. Both the new and delete operators can be used. But if something is forgotten or maybe the debugger exits early, it should in principle release everything fine. If there is a bug around it, good luck finding it! Thank you for the suggestion.
Reference to "std::unique_ptr" is here:
std::unique_ptr - cppreference.com[
4]
std::unique_ptr::operator[] - cppreference.com[
5]
How to reference the unique_ptr is here:
std::unique_ptr::get - cppreference.com[
6]
Reference to delete and new still being used is here:
std::unique_ptr::release - cppreference.com[
7]
I had noticed that in a nested class, that I was unable to instantiate a variable of the new class type. The solution is to make sure that the constructor is defined first, or else the compiler has no reference on how to instantiate the variable when it comes to it. It turns out that the issue is covered here:
class CCDCDirectControls : private CView
{
public:
CCDCDirectControls();
~CCDCDirectControls();
class Button
{
public:
Button();
~Button();
};
Button b1;
}
When building this up, it may not be necessary to make a constructor for every nested class from this reference:
c++ - In which cases there is no constructor at all, even a default constructor? - Stack Overflow[
8]
There is still a question of why one would use the nested class to begin with. A brief explanation of the "why" behind it is here:
Inner class - Wikipedia, the free encyclopedia[
9]
A nice reference to nested class scope is here:
C++ Annotations Version 10.5.0[
10]