-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVariable.cpp
111 lines (93 loc) · 2.25 KB
/
CVariable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "CVariable.h"
#include <cstring>
#include <iostream>
CVariable::CVariable() : m_xValue{}, m_sName{NULL}
{}
CVariable::CVariable(const char* name, const CMatrix& v) : m_xValue{v}, m_sName{NULL}
{
//Set the name
SetName(name);
}
CVariable::CVariable(const char*name, const double& d) : m_xValue{d}, m_sName{NULL}
{
//Set the name
SetName(name);
}
CVariable::~CVariable()
{
if (m_sName != NULL)
{
delete [] m_sName;
m_sName = NULL;
}
}
CVariable::CVariable(const CVariable& var) : m_sName{NULL}
{
SetName(var.m_sName);
//Copy the value
m_xValue = var.m_xValue;
}
const CVariable& CVariable::operator=(CVariable&& var) //overload = for other variables
{
//Check for self-assignment
if (var == *this)
{
return *this;
}
m_xValue = var.m_xValue;
if (m_sName == NULL && var.m_sName != NULL)
{
//swap pointers, since this is a temporary object and will be deleted anyways.
m_sName = var.m_sName;
var.m_sName = 0;
}
return *this; //Return this object.
}
const CVariable& CVariable::operator=(const CVariable& var) //overload = for other variables
{
//Check for self-assignment
if (var == *this)
{
return *this;
}
//Set the value of the variable.
m_xValue = var.m_xValue;
//If I don't have a name already, give me the one in the other object.
if (m_sName == NULL)
{
SetName(var.m_sName);
}
return *this; //Return this object.
}
const CVariable& CVariable::operator=(const CMatrix& m)
{
m_xValue = m;
return *this;
}
const CVariable& CVariable::operator=(const double& m)
{
m_xValue = m;
return *this;
}
bool CVariable::SetName(const char* name)
{
//Allocate enough memory for this new name.
char* newname = new char [strlen(name)];
//Check for a failure to allocate
if (newname == NULL)
return false;
//Copy
strcpy(newname, name);
//Destroy the old name string
if (m_sName != NULL)
delete [] m_sName;
//And hand over the pointer
m_sName = newname;
return true;
}
void CVariable::Clear()
{
delete [] m_sName;
m_sName = NULL;
m_xValue.resize(0,0); //Set my matrix to null.
}