簡體   English   中英

模板化類中的模板化成員函數的定義(C ++)

[英]Definition of templated member function in templated class (C++)

我有以下模板化類,在.hpp文件中聲明,實現在.hpp文件末尾的.inl文件中。 它具有模板化副本構造函數,但我不知道也找不到在.inl文件中實現模板化副本構造函數的正確語法。 有誰知道正確的語法嗎?

Foo.hpp的內容

template <class X>
class Foo
{
public:
    explicit Foo(Bar* bar);    

    //I would like to move the definition of this copy ctor to the .inl file
    template <class Y> explicit Foo(Foo<Y> const& other) :
       mBar(other.mBar)
    {
      assert(dynamic_cast<X>(mBar->someObject()) != NULL);
      //some more code
    }

    void someFunction() const;

private:
    Bar* mBar;
}
#include Foo.inl

Foo.inl的內容

template <class X>
Foo<X>::Foo(Bar* bar) : 
   mBar(bar)
{
   //some code
}

template <class X>
Foo<X>::someFunction()
{
    //do stuff
}

對於構造函數,您有兩個嵌套模板,並且在.inl文件中定義它們時必須同時指定兩者:

template <class X>
template <class Y>
Foo<X>::Foo(Foo<Y> const& other) : mBar(other.mBar) {
   assert(dynamic_cast<X>(mBar->someObject()) != NULL);
   //some more code
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM