void CMatrix::SetValues(float m[16])
{
M = m;
}
I get the error:
error C2440: ''='' : cannot convert from ''float []'' to ''float [16]''
still M is defined as float[16]
what should I do?
thanx,
Marty
simple question
I have a little problem. I have a class called matrix and a function in it that sets the values. This is it:
The name of an array is a pointer to that array, pass float* m instead of float m[16] but only if the original array still exists since you are passing a pointer.
Or do a 'for' loop to assign each value of the array.
[edited by - _Idan_ on January 16, 2004 10:19:14 AM]
Or do a 'for' loop to assign each value of the array.
[edited by - _Idan_ on January 16, 2004 10:19:14 AM]
std::copy_n(m, 16, M);
Enigma
EDIT: got m's the wrong way round!
[edited by - Enigma on January 16, 2004 10:16:56 AM]
Enigma
EDIT: got m's the wrong way round!
[edited by - Enigma on January 16, 2004 10:16:56 AM]
quote:Original post by Enigma
std::copy_n(m, 16, M);
Enigma
EDIT: got m''s the wrong way round!
[edited by - Enigma on January 16, 2004 10:16:56 AM]
cool, I''ve always wondered if there was a c++ eqivalent of memcpy
cheers.
Just wondering is there much difference between using memcpy or std::copy_n for copying arrays? Is one more efficient than the other?
Hmm, sorry - looks like copy_n is nonstandard, the standard copy algorithm is std::copy(sourceBegin, sourceEnd, destBegin), so for the above this would be:
There is absolutely no reason for std::copy to be any less efficient than std::memcpy for basic types. It is also safer, you don''t need to remember to copy (count * sizeof(type)) bytes, just count items.
Also, take the following example, with a reference counted class:
memcpy destroys the reference counts and leaks memory but copy correctly calls the Test assignment operator.
Finally, you shouldn''t worry about the performance of small things like this unless you know it''s a bottleneck, if it''s only called 10 times in your program, who cares if it takes 1 millisecond to run instead of 10 milliseconds?
Enigma
std::copy(m, m + 16, M);
There is absolutely no reason for std::copy to be any less efficient than std::memcpy for basic types. It is also safer, you don''t need to remember to copy (count * sizeof(type)) bytes, just count items.
Also, take the following example, with a reference counted class:
#include <algorithm>#include <iostream>#include <memory>class Test{ public: Test() { refCount = new int[1]; *refCount = 1; std::cout << "Create @ " << (int)refCount << " : " << *refCount << "\n"; } Test(const Test& t) { refCount = t.refCount; (*refCount)++; std::cout << "Copy @ " << (int)refCount << " : " << *refCount << "\n"; } ~Test() { (*refCount)--; std::cout << "Destroy @ " << (int)refCount << " : " << *refCount << "\n"; if (*refCount == 0) { delete[] refCount; std::cout << "Killed @ " << (int)refCount << "\n"; } } void print() { std::cout << "Print @ " << (int)refCount << " : " << *refCount << "\n"; } const Test& operator=(const Test& t) { this->~Test(); new(this) Test(t); return *this; } private: int* refCount;};int main(int, char**){ Test* t = new Test[2]; Test* t2 = new Test[2]; std::cout << "--begin memcpy--\n"; std::memcpy(t2, t, 2 * sizeof(Test)); t[0].print(); t[1].print(); t2[0].print(); t2[1].print(); std::cout << "--end memcpy--\n\n"; t2 = new Test[2]; std::cout << "--begin copy--\n"; std::copy(t, t + 2, t2); t[0].print(); t[1].print(); t2[0].print(); t2[1].print(); std::cout << "--end copy--\n";}
;memcpy destroys the reference counts and leaks memory but copy correctly calls the Test assignment operator.
Finally, you shouldn''t worry about the performance of small things like this unless you know it''s a bottleneck, if it''s only called 10 times in your program, who cares if it takes 1 millisecond to run instead of 10 milliseconds?
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement