On 07/01/2005, Victor Bazarov wrote:
[color=blue]
> First of all, make sure that your compiler is not a faulty one. Below
> is the code and the output I think it should produce (and it does with
> at least one of the compilers I have here).[/color]
BCB3 and BCB6 won't even compile this. See below. Unfortunately BCB3
and BCB6 are compilers that this code needs to support. I'll see what
gcc makes of this code later today.
[color=blue]
> I don't know how you should proceed. You could try overloading your
> function based on all possible containers. You could also name those
> functions differently (to be used in the derived classes differently).[/color]
The problem is that the code is intended to be generic. I can easily
make it work if I know in advance the names of the classes the code is
required to accomodate.
[color=blue]
> Honestly, though. Just spend a bit more time tinkering, and you'll
> get it, I am sure of it.[/color]
I'll get something that works. But I want something that's simple,
effective, easy to maintain. To achieve this I'll need to learn more
about templates. When I've got a few moments I think I need to have a
look at Andrei Alexandrescu's book.
[color=blue]
> ----------------------------[/color]
[color=blue]
> #include <iostream>
> #include <vector>
>
> template<class T> int getCount(T t)
> {
> return -1;
> }
>
> template<class T> int getCount(std::vector<T> const& vt)
> {
> return vt.size();
> }
>
> struct B
> {
> virtual int getCount() const = 0;
> };
>
> template<class T> struct D1 : B
> {
> int getCount() const { return ::getCount(db); }[/color]
BCB3&6 error: Ambiguity between 'getCount(const
std::vector<int,std::allocator<int> >)' and 'getCount(const
std::vector<int,std::allocator<int> > &)'.
[color=blue]
> T db;
> };
>
> struct D2 : B
> {
> int getCount() const { return ::getCount(db); }
> int db;
> };
>
> int main()
> {
> B *b1 = new D1<std::vector<int> >;
> B *b2 = new D2;
> std::cout << "D1::getCount returned " << b1->getCount() << std::endl;
> std::cout << "D2::getCount returned " << b2->getCount() << std::endl;
> return 0;
> }
> ---------------------------- output:
> D1::getCount returned 0
> D2::getCount returned -1
> -----------------------------------------[/color]
--
Simon Elliott
http://www.ctsn.co.uk