I have an class which I wish to instantiate by passing an array of values. The object here has two members but this has been reduced for illustration. In the future I will read values from disk and then create an object from those values, hence the array. The object will have multiple pointers later on hence the shared_ptr.
Firstly, I would like to know if this would prevent memory leaks as is. Secondly, I would like to know if there is are less bloated ways of instantiating objects and then determinsitically destroying them later on.
Class Header file:
//MyClass.hpp
#pragma once
#include "stdafx.h"
#include <array>
class SimpleClass{
private:
//Per object
double var1;
double var2;
public static:
//For calling when using templates such as std::array<>
//Using __int8 as will not be > 256
static const uint8_t varCount 2;
SimpleBody(std::array<double, varCount> inputArray);
~SimpleBody();
}
Class Implementation
//MyClass.cpp
#pragma once
#include "stdafx.h"
#include <array>
#include "body.hpp"
SimpleBody::SimpleBody(std::array<double, SimpleBody::varCount> inputArray ) {
//Assign var1
MyClass::var1= inputArray[0];
//Assign var2
MyClass::var2= inputArray[1];
};
SimpleBody::~SimpleBody() {
//Add in code here when children need deleting
};
Entry Point
// EntryPoint.cpp
//
#include "stdafx.h"
#include "MyClass.hpp"
#include <array>
#include <memory>
int main()
{
//Create an array with a smart pointer for memory management
std::unique_ptr< std::array<double, MyClass::varCount> > initArray =
std::make_unique< std::array<double, MyClass::varCount> >();
//Define values
*initArray = { 1.0,2.0 };
//Use array to create object
std::shared_ptr<MyClass> object = std::make_shared<MyClass>(*initArray );
//Free memory
initArray.reset();
object.reset();
return 0;
}