Hi,
I want to know if it is possible to create a QGenericMatrix of my own Object Type.
Like this:
Qt Code:
QGenericMatrix<5,5,MyClass> matrix;To copy to clipboard, switch view to plain text mode
thanks in advance
Hi,
I want to know if it is possible to create a QGenericMatrix of my own Object Type.
Like this:
Qt Code:
QGenericMatrix<5,5,MyClass> matrix;To copy to clipboard, switch view to plain text mode
thanks in advance
isn't that the whole meaning of 'generic'?
Isn't it faster to try than to ask here?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
I tried it but the compiler doesn't like it....
Qt Code:
class MyClass { private: QString value; public: MyClass(); bool operator== ( const MyClass & other ) const; };To copy to clipboard, switch view to plain text modeQt Code:
#include "myclass.h" MyClass::MyClass() { } value(value) { } bool MyClass::operator ==(const MyClass & other) const { return (value == other.getValue()); }To copy to clipboard, switch view to plain text mode
Qt Code:
int main(int argc, char *argv[]) { QGenericMatrix<4,4,MyClass>m; return a.exec(); }To copy to clipboard, switch view to plain text mode
You should implement assignment operator for your class.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Qtonimo (8th August 2012)
I would imagine assignment is implicit.
More likely there are problems with +, *, / etc.
If op would show the compiler error then we would know for sure...
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
Nope, the problem is in QGenericMatrix::setToIdentity uses assignment operator which looks like ... operator = (qreal other).
But I agree with you that other common operators have to be reloaded too.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
he just needs another ctor then for that issue. The assignment will be implicit
Qt Code:
class MyClass { private: QString value; public: MyClass(); MyClass(float f); // <<<<<<< bool operator== ( const MyClass & other ) const; };To copy to clipboard, switch view to plain text mode
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
Well, yes it's another way to solve the issue, but in this case a new temporary object will be created. In the case of using assignment operation that will not happen.
Implicit anything it's evil.
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
I tried it with MyClass(float f) and it works...thanks
@spirit maybe you can show me the assignment operator in this example....
is this correct?
Qt Code:
MyClass & MyClass::operator= ( const MyClass & other ) { this->value = other.getValue(); return *this; }To copy to clipboard, switch view to plain text mode
This code is correct but not very useful: it is probably equivalent to what the compiler generates automatically.
What spirit and amleto pointed out is this:
MyClass needs a way to be assigned a qreal. One way to do that is to add a constructor MyClass(qreal), which you did. However this may be suboptimal, as assigning a qreal to a MyClass first instanciates a temporary MyClass with MyClass(qreal), then assigns it with operator(const MyClass &), then destroys the temporary. You could add an assignment operator operator=(qreal) to avoid this unnecessary construction and destruction. However since MyClass is essentially a wrapper around QString which itself uses implicit sharing, I am not convinced it will make a huge difference.
Also, it is possible that some other operators (such as binary arithmetic one) are missing from MyClass which are needed for QGenericMatrix to function properly. Errors will probably show up as soon as you try arithmetic operations on your matrices.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
Here we go:
Qt Code:
... MyClass &operator=(qreal other) { return *this; } ...To copy to clipboard, switch view to plain text mode
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
Qtonimo (9th August 2012)
Qtonimo (9th August 2012)
hmmm, ok, I may have 'jumped the gun' with that comment, suggesting rvo when it does not apply.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
Thank you guys
You brought light into the darkness.
What I have to change, if I want to store pointers in my QGenericMatrix?
Qt Code:
QGenericMatrix<4,4,MyClass*> m;To copy to clipboard, switch view to plain text mode
The compiler cannot convert float to a pointer in assignment at setToIdentity()... The message is clear, but I'm not able to work arround this.
Pursue your targets and don't give up....
Well, it's gonna be challenging...
So, I think, you have chosen wrong type of container -- What are you using it for?
Qt Assistant -- rocks!
please, use tags [CODE] & [/CODE].
That wont work. You will need to create a wrapper.
If you want QGenericMatrix<4,4,T>, then it must support
T t;
t = 1.0f
since T is MyClass*, there is no opportunity to overload operators.
Therefore you are left with using a thin wrapper or keeping with value semantics.
Qt Code:
template<class T> class Wrapper { public: Wrapper(T* ptr) : ptr_(ptr) {} Wrapper& operator=(float f) { (*ptr) = f; // operator= (or suitable ctor) must be supported by T! } private: T* ptr_; };To copy to clipboard, switch view to plain text mode
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
Pointers where my intention, because I receive lots of pointers (MyClass* pointer1, MyClass* pointer2, etc.) and want to insert them in the matrix.
If I could store pointers I would save insertion time (matrix(0,0) = pointer1...).
So I have to create every incoming myclass object on the stack and delete the pointer object on the heap : (
Pursue your targets and don't give up....
Bookmarks