Getting a vector into a function that expects a vector<Base*>

后端 未结 9 1873
日久生厌
日久生厌 2020-11-29 07:11

Consider these classes.

class Base
{
   ...
};

class Derived : public Base
{
   ...
};

this function

void BaseFoo( std::ve         


        
9条回答
  •  天涯浪人
    2020-11-29 07:56

    Taking Matt Price's answer from above, given that you know in advance what types you want to use with your function, you can declare the function template in the header file, and then add explicit instantiations for those types:

    // BaseFoo.h
    template
    void BaseFoo( const std::vector& vec);
    
    // BaseFoo.cpp
    template
    void BaseFoo( const std::vector& vec);
    {
     ...
    }
    
    // Explicit instantiation means no need for definition in the header file.
    template void BaseFoo ( const std::vector& vec );
    template void BaseFoo ( const std::vector& vec );
    

提交回复
热议问题