It seems that BoVector rhs1 and BoVector rhs2 can assign all elements by a = square(bv); successfully. But it might not be a case in a common assignment of one array to another, say
int b[2]{ 1,2,3 };
int c[2]{ 3,4,5 };
c = b; //it doesn't work
c =b; will generate a compiler error. Why is that? since the code below, doesn't overwrite the operator= to be capable of array assignment.
P.S: Please DO NOT ask me what not to use or use in the comment section (this is like asking me delete the question and forget about it!). My question is why inside the class under the hood assigning arrays work but in a regular way it doesn't work!
template <typename T>
T square(T x)
{
return x * x;
}
template<typename T>
class BoVector
{
private:
T arr[1000];
size_t size;
public:
BoVector():
size(0)
{}
void push(T x)
{
this->arr[this->size] = x;
++(this->size);
}
T get(int i) const
{
return arr[i];
}
size_t getSize() const { return this->size; };
void print() const
{
for (size_t i=0;i<size;++i )
{
std::cout << this->arr[i] << '\n';
}
}
};
template<typename T>
BoVector<T> operator*(const BoVector<T>& rhs1, const BoVector<T>& rhs2 )
{
BoVector<T> result;
for (size_t i = 0; i < rhs1.getSize(); ++i)
{
result.push(rhs1.get(i) * rhs2.get(i));
}
return result;
}
int main()
{
BoVector<int> bv;
bv.push(2);
bv.push(10);
bv.push(3);
bv.push(20);
BoVector<int> a;
a = square(bv); //it works perfectly
a.print();
int b[2]{ 1,2,3 };
int c[2]{ 3,4,5 };
c = b; //it doesn't work
}