two classes derived from the same template class and operator =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class A
{
	// is abstract
};

class B : public A
{
	// ...
};

class C : public A
{
	// ...
};

template<class T> class Foo
{
public:
	Foo() {}
	virtual ~Foo() { delete _obj; }
	void createObj() { if ( !_obj ) _obj = new T; }
	A* getObj() const { return _obj; }

	template<class U>
	Foo<T>& operator=(const Foo<U>& other)
	{
		// ...
		return *this;
	}

private:
	A* _obj;
};


class FooC;

class FooB : public Foo<B>
{
public:
	FooB() {}
	virtual ~FooB() {}

	FooB& operator=(const FooC& other)
	{
		Foo<B>::operator=( static_cast< Foo<C> > other); // NOK
		// compiler doesnt yet know that FooC is derived from Foo<C>
		return *this;
	}
};

class FooC : public Foo<C>
{
	FooC() {}
	virtual ~FooC() {}

	FooC& operator=(const FooB& other)
	{
		Foo<C>::operator=(other); // OK
		return *this;
	}
};

int main(int argc, char* argv[])
{
}



main3.cpp: In member function ‘FooB& FooB::operator=(const FooC&)’:
main3.cpp:46:44: error: expected ‘(’ before ‘other’
main3.cpp:46:49: error: no matching function for call to ‘Foo<C>::Foo(const FooC&)’
main3.cpp:46:49: note: candidates are:
main3.cpp:19:2: note: Foo<T>::Foo() [with T = C]
main3.cpp:19:2: note:   candidate expects 0 arguments, 1 provided
main3.cpp:16:25: note: Foo<C>::Foo(const Foo<C>&)
main3.cpp:16:25: note:   no known conversion for argument 1 from ‘const FooC’ to ‘const Foo<C>&’


Is there any way to make it work?
Define your operator = in the .cpp file.
Topic archived. No new replies allowed.