namespace MATH
{
template <unsigned int Dimensions, class T>
struct Result
{
virtual void Fill(void) = 0;
};
template <unsigned int Dimensions, class T>
struct ResultBox : public Result<Dimensions, T>
{
/*...*/
void Fill(void) {}
};
template <unsigned int Dimensions, class T> struct BoundingBox;
template <unsigned int Dimensions, class T>
struct Object
{
public:
/*...*/
virtual bool Intersect(const BoundingBox<Dimensions, T> * BoundingBoxRef, Result<Dimensions, T> * ResultRef) const = 0;
};
template <unsigned int Dimensions, class T>
struct Point : public Object<Dimensions, T>
{
/*...*/
};
template <unsigned int Dimensions, class T>
struct BoundingBox : public Object<Dimensions, T>
{
public:
/*...*/
bool Intersect(const BoundingBox<Dimensions, T> * BoundingBoxRef, Result<Dimensions, T> * ResultRef) const;
};
template <unsigned int Dimensions, class T>
bool
BoundingBox<Dimensions, T>::
Intersect(const BoundingBox<Dimensions, T> * p_ptrBoundingBox,
ResultBox<Dimensions, T> * p_ptrResult) const
{
/*...*/
return false;
}
}
int main()
{
return 0;
}
Problem with templated inheritance and ABC
The basic idea is that I have a templated abstract base class (ABC) 'Object' that contains function prototypes for how inheritting geometric objects will interact with each other. These functions take as one or more parameters a templated type that inherits from another ABC 'Result'; different types that inherit from 'Result' will be passed dependant on what type of function is called and on what type of geometric objects it is called on, etc etc.
Anyway, the following code gives an error when complied with VStudio 6.0, and I can't seem to figure out how to correct it:
error C2244: 'BoundingBox<Dimensions,T>::Intersect' : unable to resolve function overload
The template support of vc 6.0 is incomplete and incorrect.
You should upgrade your compiler!
VC 2005 express works well for me.
You should upgrade your compiler!
VC 2005 express works well for me.
It appears that both your Intersect declarations specify a Result<N,T> type for the second parameter, but the implementation specifies a ResultBox<N,T> type. I haven't tested the code though, so I don't know if that's what's causing the compile error.
Quote:Original post by Zipster
It appears that both your Intersect declarations specify a Result<N,T> type for the second parameter, but the implementation specifies a ResultBox<N,T> type. I haven't tested the code though, so I don't know if that's what's causing the compile error.
Yes, this is supported (or is supposed to be). I don't do it often but I have successfully done it before. If a function in a derived class implements a pure virtual one that has as one of it's parameters an ABC, then the function protoype in the derived class should have the ABC type but the function definition should have the derived type.
edit: Not only have I done it before, but I've done it with VS6.0. The only difference was that the 'BoundingBox' class in my example was not a templated class (and as such the 'Intersect' function definition was in its own .cpp file as opposed to being in the header file), but I don't know if this is what is making it not compile. I'm hoping someone here can either tell me what is wrong or that I just can't do it.
ResultBox<Dimensions, T> != Result<Dimensions, T>
When you type the same complicated expression upteen times, you will make mistakes.
Quote:Original post by jorganderQuote:Original post by Zipster
It appears that both your Intersect declarations specify a Result<N,T> type for the second parameter, but the implementation specifies a ResultBox<N,T> type. I haven't tested the code though, so I don't know if that's what's causing the compile error.
Yes, this is supported (or is supposed to be). I don't do it often but I have successfully done it before. If a function in a derived class implements a pure virtual one that has as one of it's parameters an ABC, then the function protoype in the derived class should have the ABC type but the function definition should have the derived type.
This sounds strange to me - declaration and definition having different signatures? How can the compiler match them?
From my local copy of the VS help files: I get
Quote:Compiler Error C2244
'identifier' : unable to match function definition to an existing declaration
C2244 can also occur when an incorrect function signature is used for a member function of a class template.
C2244 can also occur when an incorrect function signature is used for a member function template.
You cannot partially specialize a function template.
(underlining is mine).
Regards,
jorgande said:Yes, this is supported (or is supposed to be). I don't do it often but I have successfully done it before. If a function in a derived class implements a pure virtual one that has as one of it's parameters an ABC, then the function protoype in the derived class should have the ABC type but the function definition should have the derived type.
I don't believe you.
Here is what your above code is doing, minus the template stuff:
struct A { virtual void foo() =0;};struct B:A { virtual void foo() {}};struct X { virtual void bar(A*) = 0;};struct Y { virtual void bar(A*);};void Y::bar(B*) {};
The above does not compile under VC2003. The above should not compile. Does VC6.0 compile the above?
Quote:edit: Not only have I done it before, but I've done it with VS6.0. The only difference was that the 'BoundingBox' class in my example was not a templated class (and as such the 'Intersect' function definition was in its own .cpp file as opposed to being in the header file), but I don't know if this is what is making it not compile. I'm hoping someone here can either tell me what is wrong or that I just can't do it.
Give me code that compiles in VC 6.0 that is essentially the same as your above post, and I'll believe you. But I honestly think you are confused.
I cannot correctly replicate the code either. Whatever I had done before isn't what I am trying to do now and I must be remembering something different. But I'm pretty sure it had to do with a parameter of a function having different types between the protoype and the definition, but that the type was a reference and one was a parent/child of the other.
Anyway, thx for the replies. Looks like it's back to the drawing board for me.
Anyway, thx for the replies. Looks like it's back to the drawing board for me.
If this is what you want to do, you can create functionally identical code using some dynamic_casts.
If we have
struct A:B;
and
struct X:Y;
with X::foo(A*), but we want Y::foo(B*), you can do the following:
You then write your code inside fooB(B*).
Note that if someone calls X::foo on a Y by passing an A instead of a B, your fooB gets passed a NULL. But really, what did you expect should happen? :)
If we have
struct A:B;
and
struct X:Y;
with X::foo(A*), but we want Y::foo(B*), you can do the following:
struct X { virtual void foo(A*) = 0;};struct Y:X { virtual void fooB(B*b) { ... }; virtual void foo(A*a) {fooB(dynamic_cast<B*>(a);}};
You then write your code inside fooB(B*).
Note that if someone calls X::foo on a Y by passing an A instead of a B, your fooB gets passed a NULL. But really, what did you expect should happen? :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement