开发者

How to define a template overide's function in a non-inline way

Hi so I have a template class called Body, that takes a single sf::Drawable descendant as a template argument, and i'm trying to overide the Render() function only for the case that the template argument is a sf::Shape.

How do i do this in a no开发者_StackOverflown-inline way? The code works when I define the function inside the class, automatically making it inline, but I get link errors (multiple Render symbols detected) when I define the function in a seperate .cpp file.

If it helps here's the code that produces an error:

// in the header file
template<typename drawable= void>
class Body : public sf::Drawable
{
    void Render(){Do_Something();
}

template <> 
class Body<Shape> : public sf::Drawable
{
    void Render();
}

// in the cpp file
void Body<Shape>::Render()
{
    Do_Something_Else();
}


You mean like this?

template <typename T> 
struct Foo {
    int frob() const;
};

// Note: Member function specializations do not require 
//       full class specializations.

template <typename T> 
int Foo<T>::frob() const { return 42; }

template <> 
int Foo<float>::frob() const { return 0xbeef; }


#include <iostream>
int main () {
    std::cout << Foo<int>().frob() << '\n';
    std::cout << Foo<float>().frob() << '\n';
}

Note that the specializations need to be visible where you use them, so in most cases, you have to put them in the header, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜