MATLAB arrays support matrix operations and element operations. For example, M*N and M.*N. This is a quite intuitive way to distinguish the two di
MATLAB arrays support matrix operations and element operations. For example, M*N and M.*N. This is a quite intuitive way to distinguish the two different operations. If I want to implement similar operations in C++, how can I do that?
Can I create a new operator, .*, too? If yes, can anyone give me some guidance?
As for the first part you can overload most of the operators and there are some that you can not overload and the list of operators in C++ are:
Arithmetic
+ (addition)- (subtraction)* (multiplication)/ (division)% (modulus)Bitwise
^ (XOR)| (OR)& (AND)~ (Complement)<< (Shift Left, Insertion to Stream)>> (Shift Right, Extraction from Stream)Assignment
= (Assignment)Relational
== (Equality)!= (Inequality)> (Greater-Than)< (Less-Than)>= (Greater-Than Or Equal-To)<= (Less-Than Or Equal-To)Logical
! (NOT)&& (AND)|| (OR)Compound Assignment
+= (Addition-Assignment)-= (Subtraction-Assignment)*= (Multiplication-Assignment)/= (Division-Assignment)%= (Modulus-Assignment)&= (AND-Assignment)|= (OR-Assignment)^= (XOR-Assignment)<<= (Shift-Left Assignment)>>= (Shift-Right Assignment)Increment - Decrement - Both have 2 forms (prefix) and (postfix)
++ (Increment)-- (Decrement)Subscript
[] (Subscript)Function Call
() (Function Call)Address, Reference, Pointer
operator&()operator*()operator->()Comma
operator,()Member Reference
operator->()operator->*()Memory Management
newdeletenew[]delete[]Conversion
operator "type" () constNON Modifiable Operators - Operators that can not be overloaded
?: (Conditional - Ternary). (Member Selection).* (Member Selection With Pointer To Member):: (Scope Resolution)sizeof() (Object Size Information)typeid() (Object Type Information)So knowing this list will help to answer your questions. Can you Create a "New Operator" in C++? No! If you want to implement similar operations in C++; how can I do that?
You have 4 choices: Either overload an already existing operator that can be overloaded, write a function or method to do the type of calculations you want to perform, create a template type to do the work for you, or the last one which is the least common to do but you can also write macros to do them for you.
There is a header only Math API Library that is used quite frequently with OpenGL graphics API and OpenGL's Shader Language GLSL and this library has many features that work with vectors, matrices, quaternions etc., and all the necessary functions and operations that can be done to them. Here is the link to GLM You can have a look at their documentation as well as their library implementations since it is a headers only library or API. This should give you some insight on how they constructed their Vector and Matrix objects and the operations that can be done to them.