Templates and inheritance



 DEVELOP > c-Plus-Plus > Templates and inheritance

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Przemyslaw Koprowski"
Date: 30 Mar 2007 02:49:11 PM
Object: Templates and inheritance
Hi,
I have the following problem. Consider two simple classes AA and BB,
BB inherits from AA. AA contains a class A inside and a pure virtual
method getA returning an object of class A. BB overrides both.
Here is the code:
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};
class BB : public AA {
public:
class A : public AA::A {
public:
A(void) {};
};
virtual AA::A getA(void) { return BB::A(); };
virtual ~BB() {};
};
BB b;
int main(void)
{
return 0;
}
So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:
template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};
template <class T, unsigned N>
class BB : public AA<T> {
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};
};
BB<int,5> b;
int main()
{
return 0;
}
Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:
wzlsd2.cpp:19: error: type 'AA<T>' is not derived from type 'BB<T, N>'
wzlsd2.cpp:19: error: expected ';' before 'getA'
wzlsd2.cpp:23: error: cannot declare variable 'b' to be of abstract type
'BB<int, 5u>'
wzlsd2.cpp:13: note: because the following virtual functions are pure within
'BB<int, 5u>':
wzlsd2.cpp:8: note: AA<T>::A AA<T>::getA() [with T = int]
Obviously the first error is the important one. Compiler does not accept
the type AA<T>::A, althougth it accepted it just few lines above (when
declaring class BB<T,N>::A).
And my question is: what's wrong? What mistake I'm doing here?
Or is this a compiler's bug?
TIA,
Przemek
.

User: "Michael"

Title: Re: Templates and inheritance 30 Mar 2007 02:02:49 PM
On Mar 30, 12:49 pm, Przemyslaw Koprowski <o...@siggraph.pkoprowski>
wrote:

So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:

template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};

};

template <class T, unsigned N>
class BB : public AA<T> {
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};

};

BB<int,5> b;

int main()
{
return 0;

}

Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:

And my question is: what's wrong? What mistake I'm doing here?
Or is this a compiler's bug?

For what it's worth, this compiles and runs on Visual Studio. That
doesn't necessarily mean it's a compiler bug in g++, it could be an
extension or a bug in VS.
Michael
.

User: "red floyd"

Title: Re: Templates and inheritance 30 Mar 2007 01:52:42 PM
Przemyslaw Koprowski wrote:

Hi,

I have the following problem. Consider two simple classes AA and BB,
BB inherits from AA. AA contains a class A inside and a pure virtual
method getA returning an object of class A. BB overrides both.
Here is the code:

class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

class BB : public AA {
public:
class A : public AA::A {
public:
A(void) {};
};
virtual AA::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB b;

int main(void)
{
return 0;
}

So far, so good. Everything goes fine. But now suppose that we
change the classes AA/BB into class *templates*:

template <class T>
class AA {
public:
class A {
public:
A(void) {};
};
virtual A getA(void) = 0;
virtual ~AA() {};
};

template <class T, unsigned N>
class BB : public AA<T> {
public:
class A : public AA<T>::A {
public:
A(void) {};
};
virtual AA<T>::A getA(void) { return BB::A(); };
virtual ~BB() {};
};

BB<int,5> b;

int main()
{
return 0;
}

Although I didn't do any other changes, I cannot compile it
(with GNU C++ 4.1.0). I receive the following errors:

wzlsd2.cpp:19: error: type 'AA<T>' is not derived from type 'BB<T, N>'
wzlsd2.cpp:19: error: expected ';' before 'getA'
wzlsd2.cpp:23: error: cannot declare variable 'b' to be of abstract type
'BB<int, 5u>'
wzlsd2.cpp:13: note: because the following virtual functions are pure within
'BB<int, 5u>':
wzlsd2.cpp:8: note: AA<T>::A AA<T>::getA() [with T = int]

Obviously the first error is the important one. Compiler does not accept
the type AA<T>::A, althougth it accepted it just few lines above (when
declaring class BB<T,N>::A).

virtual typename AA<T>::A getA().
And remove the BB:: inside getA
virtual typename AA<T>::A getA() { return A(); }
.
User: "Przemyslaw Koprowski"

Title: Re: Templates and inheritance 30 Mar 2007 03:28:54 PM
On Fri, 30 Mar 2007 18:52:42 +0000, red floyd wrote:

virtual typename AA<T>::A getA().

And remove the BB:: inside getA

virtual typename AA<T>::A getA() { return A(); }

Thanks. It works, now.
Przemek
.
User: "red floyd"

Title: Re: Templates and inheritance 30 Mar 2007 03:17:08 PM
Przemyslaw Koprowski wrote:

On Fri, 30 Mar 2007 18:52:42 +0000, red floyd wrote:

virtual typename AA<T>::A getA().

And remove the BB:: inside getA

virtual typename AA<T>::A getA() { return A(); }

Thanks. It works, now.

Przemek

By the way, I believe g++ complaining about BB:A() is an error in g++.
Comeau likes
virtual typename AA<T>::A getA() { return BB::A(); }
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER