473,395 Members | 1,675 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,395 software developers and data experts.

Templates and helpers.

I want to be able to define a super class that defines some basic functionality
and also some helpers that define, mainly, arithmetic operations, but I've been
having some trouble.

The following example illustrates what i want to do:

template<typename T = int>
class A{};

template<typename T = int>
class B: public A<T>{};

template<typename T>
A<T> operator*(const A<T>& lhs, const A<T>& rhs){
return lhs;
}

int main(){
B<> b_1;
B<> b_2;
B<> b_3;
b_3 = b_1*b_2;
}

But when I compile it I get the following:
tst.cc: In function `int main()':
tst.cc:16: error: no match for 'operator=' in 'b_3 = operator* [with T
=int](((const A<int>&)((const A<int>*)((A<int>*)(&b_1)))), ((const A<int>&)
((const A<int>*)((A<int>*)(&b_2)))))'
tst.cc:5: note: candidates are: B<int>& B<int>::operator=(const B<int>&)

Which I think means that the assignment operator ain't defined so that it's possible
to assign an instance of class A to class B. I was hoping that the inheritance would
make this possible, but alas.

What i then did was to define the operator as:
template<typename T>
T operator*(const T& lhs, const T& rhs){
return lhs;
}

Which works, but won't this cause a problem if i try multiplying something else
since this is quite a general solution? What I wanted to do was to be able to just
add some functionality in my sub-classes and be able to use the helpers that i
defined in the super class, and be able to use the same scheme with another
set of classes (i.e D: public D). But if i do that won't that mean that the two
functions will "collide"?

Thanks in advance.
--
(Should insert humorous quotation here)
Oct 6 '05 #1
4 1424
Magnus Jonneryd wrote:
I want to be able to define a super class that defines some basic functionality
and also some helpers that define, mainly, arithmetic operations, but I've been
having some trouble.

The following example illustrates what i want to do:

template<typename T = int>
class A{};

template<typename T = int>
class B: public A<T>{};

template<typename T>
A<T> operator*(const A<T>& lhs, const A<T>& rhs){
return lhs;
}

int main(){
B<> b_1;
B<> b_2;
B<> b_3;
b_3 = b_1*b_2;
}

But when I compile it I get the following:
tst.cc: In function `int main()':
tst.cc:16: error: no match for 'operator=' in 'b_3 = operator* [with T
=int](((const A<int>&)((const A<int>*)((A<int>*)(&b_1)))), ((const A<int>&)
((const A<int>*)((A<int>*)(&b_2)))))'
tst.cc:5: note: candidates are: B<int>& B<int>::operator=(const B<int>&)
This isn't going to work even without templates. There is no assignment
operator in B that takes A as its argument.
Which I think means that the assignment operator ain't defined so that it's possible
to assign an instance of class A to class B.
Yep. Templates have nothing to do with it.
I was hoping that the inheritance would
make this possible, but alas.
It never does.
What i then did was to define the operator as:
template<typename T>
T operator*(const T& lhs, const T& rhs){
return lhs;
}

Which works, but won't this cause a problem if i try multiplying something else
since this is quite a general solution?
Yes, any other user-defined type suddenly has operator* defined for it.
What I wanted to do was to be able to just
add some functionality in my sub-classes and be able to use the helpers that i
defined in the super class, and be able to use the same scheme with another
set of classes (i.e D: public D). But if i do that won't that mean that the two
functions will "collide"?


I don't understand what functions will collide. If you define the
required assignment operator (and define it as doing the right thing),
what should collide with what?

V
Oct 6 '05 #2
Victor Bazarov wrote:
Magnus Jonneryd wrote:
I want to be able to define a super class that defines some basic
functionality and also some helpers that define, mainly, arithmetic
operations, but I've been having some trouble.

The following example illustrates what i want to do:

template<typename T = int>
class A{};

template<typename T = int>
class B: public A<T>{};

template<typename T>
A<T> operator*(const A<T>& lhs, const A<T>& rhs){
return lhs;
}

int main(){
B<> b_1;
B<> b_2;
B<> b_3;
b_3 = b_1*b_2;
}

But when I compile it I get the following:
tst.cc: In function `int main()':
tst.cc:16: error: no match for 'operator=' in 'b_3 = operator*
[with T =int](((const A<int>&)((const
A<int>*)((A<int>*)(&b_1)))), ((const A<int>&) ((const
A<int>*)((A<int>*)(&b_2)))))' tst.cc:5: note: candidates are:
B<int>& B<int>::operator=(const B<int>&)


This isn't going to work even without templates. There is no assignment
operator in B that takes A as its argument.
Which I think means that the assignment operator ain't defined so that
it's possible to assign an instance of class A to class B.


Yep. Templates have nothing to do with it.
I was hoping that the inheritance would
make this possible, but alas.


It never does.
What i then did was to define the operator as:
template<typename T>
T operator*(const T& lhs, const T& rhs){
return lhs;
}

Which works, but won't this cause a problem if i try multiplying
something else since this is quite a general solution?


Yes, any other user-defined type suddenly has operator* defined for it.
> What I wanted to do was to be able to just
add some functionality in my sub-classes and be able to use the helpers
that i defined in the super class, and be able to use the same scheme
with another set of classes (i.e D: public D). But if i do that won't
that mean that the two functions will "collide"?


I don't understand what functions will collide. If you define the
required assignment operator (and define it as doing the right thing),
what should collide with what?

V


Uhm, nevermind about the "collision" bit, I was just rambling. But as a
follow-up question: If i define an assignment operator, like so:

B<T> operator=(const A<T>& a){
if(this==&a)
return *this;

return B<T>(a);
}

How come it's possible to assign like this:
B<> b;
B<> b_1;
b = b_1;

As far as i can tell The class B inherits from A making it possible to make
the function call but since I don't know if it's the call: B = B or
B = A; I can't assign the extra information in B (if it's a case of B = B)
,on the left hand side, which means I loose some information every time i
use the operator. Is this correct or am I just rambling on again?

Thanks again.
--
(Should insert humorous quotation here)
Oct 6 '05 #3
Magnus Jonneryd wrote:
[...] a
follow-up question: If i define an assignment operator, like so:

B<T> operator=(const A<T>& a){
if(this==&a)
return *this;

return B<T>(a);
}

How come it's possible to assign like this:
B<> b;
B<> b_1;
b = b_1;
The user-defined assignment from A<T> *overloads* the compiler-defined
assignment from B.
As far as i can tell The class B inherits from A making it possible to make
the function call but since I don't know if it's the call: B = B or
B = A; I can't assign the extra information in B (if it's a case of B = B)
,on the left hand side, which means I loose some information every time i
use the operator. Is this correct or am I just rambling on again?


I don't know if you're rambling. Define the copy-assignment operator
and see which one is called when. Yes, you don't know whether the A<T>
is part of another B or of something completely different. That's why
A<T> is the only thing you can rely upon. The other stuff (data beyond
A<T> in 'this' B) has to either be kept as is (which is better) or
made up from scratch.

V
Oct 6 '05 #4
Victor Bazarov wrote:
Magnus Jonneryd wrote:
[...] a
follow-up question: If i define an assignment operator, like so:

B<T> operator=(const A<T>& a){
if(this==&a)
return *this;

return B<T>(a);
}

How come it's possible to assign like this:
B<> b;
B<> b_1;
b = b_1;


The user-defined assignment from A<T> *overloads* the compiler-defined
assignment from B.
As far as i can tell The class B inherits from A making it possible to
make the function call but since I don't know if it's the call: B = B or
B = A; I can't assign the extra information in B (if it's a case of B =
B) ,on the left hand side, which means I loose some information every
time i use the operator. Is this correct or am I just rambling on again?


I don't know if you're rambling. Define the copy-assignment operator
and see which one is called when. Yes, you don't know whether the A<T>
is part of another B or of something completely different. That's why
A<T> is the only thing you can rely upon. The other stuff (data beyond
A<T> in 'this' B) has to either be kept as is (which is better) or
made up from scratch.

V


Thanks, I think that straightened it out.
--
(Should insert humorous quotation here)
Oct 6 '05 #5

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

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
4
by: Jeff Jarrell | last post by:
I am working with Winforms and writing some general purpose helpers for use with a 3rd party grid (devexpress). I have been using BindingList<Tto bind to the grid. At some point I need to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.