On Mar 23, 8:37 am, "Zilla" <zill...@bellsouth.net> wrote:
On Mar 22, 5:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Zillawrote:
On Mar 22, 4:06 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
[..]
class D1 : public A<D1> { // derived one
public:
// constructors are not inherited, so you need one here
D1(int v) : A<D1>(v) {}
};
class D2 : public A<D2> { // derived two
public:
// constructors are not inherited, so you need one here
D2(int v) : A<D2>(v) {}
};
[..]
Would that work? Use it as a starting point.
V
Thanks, so far it worked for these test cases. It's scary how the
compiler knows how to implement the "=" operation. I'll code it so
as NOT to leave it to chance.
int main()
{
D1 d33(1);
D1 d22(1);
D1 d11=d33;
cout << "d11.value: " << d11.getVal() << endl;
d11=1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d11 + 1;
cout << "d11.value: " << d11.getVal() << endl;
d11 = 1 + d11;
cout << "d11.value: " << d11.getVal() << endl;
d11 = d22 + d11;
cout << "d11.value: " << d11.getVal() << endl;
return 0;
}
Just to bring it to your attention: it is important to not get the
types mixed up when copy-pasting your code. The class 'D1' is
defined as deriving from 'A<D1>' and 'D2' is from 'A<D2>'. This
is known as CRTP, IIRC. If when you define some 'D3' you make it
derive from 'A<D2>', the stuff can start going wrong and it might
be difficult to find. Or maybe not... Good luck!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide
quoted text -
- Show quoted text -
Thanks. Yes I noticed this right off. I'm playing with this code now,
and even trying out the straight-forward use of the template, ie,
without the class declarations inheritting the template.
class D1 {
blah()
};
class D2 {
blah()
};
int main()
{
A<D1> d1;
A<D2> d2;
d1=blah...
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Ok, I was having problems with your example, so I went back to basics.
I stripped it down to the following code for operator=() only. I can't
even get it to compile...
g++ -c timers.cpp
timers.cpp: In function `int main()':
timers.cpp:35: no match for `tRCD & = double'
timers.cpp:30: candidates are: class tRCD & tRCD::operator =(const
tRCD &)
Why isn't the Timer<T>::operator=(...) being seen?
#include <iostream>
////////////////////////////////////////////////////////
// Base class that defines ALL the operations
template<class T>
class Timer {
public:
Timer();
T& operator=(const double v);
double _min;
};
template<class T>
Timer<T>::Timer() : _min(0)
{
}
template<class T>
T& Timer<T>::operator=(const double v)
{
// do something
}
class tRCD : public Timer<tRCD> {
public:
tRCD() : Timer<tRCD>() {}
~tRCD() {}
};
int main()
{
tRCD t1;
t1=3.0;
cout << "t1.min: " << t1._min << endl;
}
assignment operator from the derived class. You need to add
a line with 'using' in it to the 'tRCD' class.
is going to return. I am not sure how you're going to handle
returned from the assignment op).