The following is a tiny part of my homework which I found to be wrong during the tests I wrote for the code.

I have a templated Array class
In that class, I have function named get which returns a refernce to the Template, It's signature is :

template <class T>
T& Array<T>::get(int index) 

Obviously, I want to be able to get a value from the array - and every change I do in the function, will change the original value in the array aswell.
I have the following call :

Company Hiring=companies.get(companyID);

It does returns a Company with the same data, but whenever I change it (lets say, adding a worker, changing number of employees,...) and returns, the Company in the array does not change and stays blank.

What am I doing wrong ? do you have any ideas ?

If you need more details \ code please tell me and I'll edit.

Thank you very much.

有帮助吗?

解决方案

Maybe what you mean is:

Company& hiring = companies.get(companyID);

If you're not capturing a reference in that variable, you're making a copy that will never change unless you manipulate it directly.

其他提示

What you are doing wrong is that you are making copy of the Company returned by companies.get(companyID). If you wanted to change the contents of companies, you would need to assign to a reference:

Company& Hiring = companies.get(companyID);

Hiring is a copy of the array element. Modifying that won't change the copy in the array. Instead, you want a reference to the array element:

Company& Hiring=companies.get(companyID);
       ^
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top