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].
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.
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.
Bookmarks