void IntArray::Insert(int valueToInsert, int indexToInsert = this->GetLength() + 1)
{
int* changedArray = newint[this->GetLength() + 1];
for (int i = 0; i < this->GetLength() + 1; ++i)
{
if (this->m_arrayPointer[i])
{
changedArray[i] = this->m_arrayPointer[i];
}
}
if (indexToInsert != this->GetLength() + 1)
{
for (int i = this->GetLength() + 1; i > indexToInsert; --i)
{
changedArray[i] = changedArray[i-1];
}
}
changedArray[indexToInsert] = valueToInsert;
delete[] this->m_arrayPointer;
m_arrayPointer = changedArray;
m_arrayLength = this->GetLength();
}
I receive an error when building this code, which is the subject:
'this' may not be used in this context
I think that I would have to check it inside the body of the function by checking if the parameter is null, and then setting valueToInsert to GetLength() + 1 if it is.
Which is the line number for the error? Line 1 looks awkward, as I think the default values need to be compile-time deducible? Nope.
Anyway, you don't need to abuse this-> the way you do.
So maybe a simple solution could be deleting them all. this-> is useful when a local variable has the same name as a member variable of the class.
Which one's are unnecessary? All of them serve a purpose in my eyes.
But you don't need them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct Numbers
{
int a, b;
// this-> needed in here
void set_a(int a)
{
this->a = a;
}
// this-> not needed in here
void set_ab(int to_ab)
{
set_a(to_ab);
b = to_ab;
}
};
void IntArray::Insert(int valueToInsert, int indexToInsert = this->GetLength() + 1)
{
int* changedArray = newint[this->GetLength() + 1];
for (int i = 0; i < this->GetLength() + 1; ++i)
{
if (this->m_arrayPointer[i])
{
changedArray[i] = this->m_arrayPointer[i];
}
}
if (indexToInsert != this->GetLength() + 1)
{
for (int i = this->GetLength() + 1; i > indexToInsert; --i)
{
changedArray[i] = changedArray[i-1];
}
}
changedArray[indexToInsert] = valueToInsert;
delete[] this->m_arrayPointer;
m_arrayPointer = changedArray;
m_arrayLength = this->GetLength();
}
Here you are trying to copy non-zero elements of the original array, are not you?
1 2 3 4 5 6 7
for (int i = 0; i < this->GetLength() + 1; ++i)
{
if (this->m_arrayPointer[i])
{
changedArray[i] = this->m_arrayPointer[i];
}
}
However the original array has no an element with index this->GetLength(). So you have already gotten undefined behavior. Further you are coping non-zero elements but what about other elements of the new array? What values will it have?!
The next loop contains the same bug.
1 2 3 4
for (int i = this->GetLength() + 1; i > indexToInsert; --i)
{
changedArray[i] = changedArray[i-1];
}
You are trying access memory outside your array because the new array has no an element with index this->GetLength() + 1
And here you did not change the length of the array
m_arrayLength = this->GetLength();
The new one shall be equal to
m_arrayLength = this->GetLength() + 1;
As for the default argument you can use for example std::string::npos
Yeah, I guess it's not necessary, but I just like having them there for some reason.
@vlad from moscow,
Good catch that I did not see there.. the length of the array does not refer to the last element's index. As for the last "flaw", I think I am fine because I updated m_arrayPointer to equal the new array, so getting it's length should be fine.
-----
In general, am I not allowed to use the this pointer in my custom parameters?
I set the m_arrayPointer to the new array, the one with the inserted element, and then set m_arrayLength to the length of the new result of this->GetResult(), which gets the length of the new assignment to m_arrayPointer.
And is this this pointer allowed to be used in parameters?
Edit: Oops, I just forgot that calling GetLength() actually returned m_arrayLength.