 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mwreese@austin.rr.com Guest
|
Posted: Fri Mar 25, 2005 11:06 am Post subject: problem with tempatized constructor vs. copy constructor |
|
|
In trying to create a class that has both a copy constructor and a
constructor that takes a functor for initialization, I get a compile
error. It seems that the compiler tries to use the constructor that
takes the functor for copy purposes rather than the copy constructor.
Is there any way to get around this problem? Below is a simple example
that illustrates the problem. Thanks for all your help.
--Michele
I'm using gcc 3.4.1 and the error message is:
cd /home/r3aajx/work/adl/
g++ -g -Wall example2.C -o out
Reading site .bashrc
Reading site .bash.interactive
example2.C: In constructor `FooImpl<S>::FooImpl(R&) [with R = Foo<20>,
int S = 20]':
example2.C:38: instantiated from `Foo<S>::Foo(R&) [with R = Foo<20>,
int S = 20]'
example2.C:49: instantiated from here
example2.C:11: error: no match for call to `(Foo<20>) ()'
Compilation exited abnormally with code 1 at Thu Mar 24 16:16:06
#include <iostream>
using namespace std;
template <int S>
class FooImpl {
public:
FooImpl() : _data(0), _size(S) {};
FooImpl(const FooImpl<S>& f) : _data(f._data), _size (f._size) {}
template <typename R>
FooImpl(R &r) { r(); }
void print(ostream &o) {
o << _data << "(" << _size << ")";
};
private:
unsigned _data;
unsigned _size;
};
class my_functor {
public:
void operator ()() { cout << "in functor " << endl; }
};
template
class Foo : private FooImpl<S> {
public:
typedef FooImpl<S> _Base;
Foo () {};
Foo (const Foo<S> &x) : _Base(x) {};
template <typename R>
Foo (R &r) : _Base(r) {}
};
int main (int argc, char **argv)
{
my_functor mf;
Foo<20> f1(mf);
Foo<20> f2(f1);
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sat Mar 26, 2005 9:37 am Post subject: Re: problem with tempatized constructor vs. copy constructor |
|
|
[email]mwreese (AT) austin (DOT) rr.com[/email] wrote:
| Quote: | In trying to create a class that has both a copy constructor and a
constructor that takes a functor for initialization, I get a compile
error. It seems that the compiler tries to use the constructor that
takes the functor for copy purposes rather than the copy constructor.
Is there any way to get around this problem? Below is a simple example
that illustrates the problem. Thanks for all your help.
--Michele
I'm using gcc 3.4.1 and the error message is:
example2.C: In constructor `FooImpl<S>::FooImpl(R&) [with R = Foo<20>,
int S = 20]':
example2.C:38: instantiated from `Foo<S>::Foo(R&) [with R = Foo<20>,
int S = 20]'
example2.C:49: instantiated from here
example2.C:11: error: no match for call to `(Foo<20>) ()'
template <int S
class FooImpl {
public:
FooImpl(const FooImpl
template <typename R
FooImpl(R &r) { r(); }
};
template
class Foo : private FooImpl
public:
typedef FooImpl<S> _Base;
Foo (const Foo<S> &x) : _Base(x) {};
template <typename R
Foo (R &r) : _Base(r) {}
};
|
The template-constructor is closer than the copy constructor, since
the templating makes it an exact match, whereas the copy ctor match
requires adding const-qualifications for the derived class. The same
problem comes up with Foo::Foo(const Foo&): the call to the base ctor
needs to do an upward cast, while the templated ctor requires no
casting.
You can work around this quickly with:
my_functor mf;
Foo<20> f1(mf);
Foo<20> f2((const Foo<20>&)f1);
And adjusting the copy ctor of the derived class:
Foo (const Foo &x) : _Base((const _Base &)x) {}
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marco Jez Guest
|
Posted: Sat Mar 26, 2005 9:46 am Post subject: Re: problem with tempatized constructor vs. copy constructor |
|
|
<mwreese (AT) austin (DOT) rr.com> ha scritto nel messaggio
news:1111702760.994997.244020 (AT) z14g2000cwz (DOT) googlegroups.com...
| Quote: | In trying to create a class that has both a copy constructor and a
constructor that takes a functor for initialization, I get a compile
error. It seems that the compiler tries to use the constructor that
takes the functor for copy purposes rather than the copy constructor.
Is there any way to get around this problem? Below is a simple example
that illustrates the problem. Thanks for all your help.
|
The copy constructor in class Foo calls FooImpl<S>(x) where x is of type
const Foo<S>&. The compiler will call the only constructor in FooImpl<S>
that accepts a const Foo<S>&, and that's your "functor-ready" constructor,
NOT the copy constructor (which accepts const FooImpl<S>& instead).
Try to make an explicit cast to the base class when calling the base
constructor from Foo's copy constructor:
Foo (const Foo<S> &x) : _Base(static_cast<const FooImpl(x)) {};
Cheers,
Marco
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|