473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Speacialized operator for dissimilar types

I can't find a solution to this one, any suggestions would be welcome.

Say you have a matrix class specialized with storage type and
dimensions:

template<class T, unsigned ROWS, unsigned COLS>
class Matrix() {
...
};

You can overload an operation for multiplication of similar specialized
types:
Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;

but can you think of a way to specialize a global function for
dissimilar types? ie:

Matrix<float, 3, 1> m1, m3;
Matrix<float, 3, 3> m2;
//Initialize operands...
m3 = m2 * m1;
TIA for any suggestions!

Jul 23 '05 #1
2 974
James wrote:
I can't find a solution to this one, any suggestions would be welcome.

Say you have a matrix class specialized with storage type and
dimensions:

template<class T, unsigned ROWS, unsigned COLS>
class Matrix() {
...
};

You can overload an operation for multiplication of similar specialized
types:
Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;

but can you think of a way to specialize a global function for
dissimilar types? ie:

Matrix<float, 3, 1> m1, m3;
Matrix<float, 3, 3> m2;
//Initialize operands...
m3 = m2 * m1;
TIA for any suggestions!


Here is a test program that I think does what you are trying to do:

-Alan

#include <iostream>

template <typename T, unsigned ROWS, unsigned COLS>
class Matrix
{
} ;

template <typename T, unsigned A, unsigned B, unsigned C>
Matrix<T, A, C> operator *(
const Matrix<T, A, B> & m1,
const Matrix<T, B, C> & m2)
{
std::cout << "Multiplying " << A << 'x' << B << " Matrix by "
<< B << 'x' << C << " Matrix." << std::endl ;
return Matrix<T, A, C>() ;
}

int main()
{
Matrix<int, 2, 3> a ;
Matrix<int, 3, 4> b ;
Matrix<int, 2, 4> c ;

c = a * b ;

return 0 ;
}
Jul 23 '05 #2
This does exactly what I want. I'll give it a shot, thanks Alan.

James

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
6
by: sandSpiderX | last post by:
Hi, How do i use this definition of overloaded operator, T& operator*(T*); like struct X {}; X ox; X* px=&ox;
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
Is there some way to get a function-pointer to the operators of the builtin types? In the following example I have a generic function which applies an operator on two values, however it does not...
130
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.