Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可继承类的模板类的强制转换_C++_Templates_Casting - Fatal编程技术网

C++ 可继承类的模板类的强制转换

C++ 可继承类的模板类的强制转换,c++,templates,casting,C++,Templates,Casting,我有Base和Derived类和模板类Container哪个参数可以是Base和Derived。我需要将Container转换为Container是否可以这样做?我应该使用哪种类型?不,这是不可能的。您必须创建一个新容器,并逐个元素进行转换 请注意,您可以这样做: #include <iostream> #include <vector> using namespace std; class A { }; class B: public A{ }; int main

我有
Base
Derived
类和模板类
Container
哪个参数可以是
Base
Derived
。我需要将
Container
转换为
Container
是否可以这样做?我应该使用哪种类型?

不,这是不可能的。您必须创建一个新容器,并逐个元素进行转换

请注意,您可以这样做:

#include <iostream>
#include <vector>
using namespace std;

class A {
};

class B: public A{

};
int main() {
    vector<B> b(5);
    vector<A> a(b.begin(), b.end());
    return 0;
}
#包括
#包括
使用名称空间std;
甲级{
};
B类:公共A{
};
int main(){
载体b(5);
向量a(b.begin(),b.end());
返回0;
}

不,这是不可能的<代码>容器不是从
容器
派生的,它们只是同一类模板的两个实例

这是有意义的:假设一个
容器
可以有效地替代一个
容器
,以替代一个需要
容器
的函数,并假设有一个派生自
但与
派生
无关的第二类
Derived2

void foo(Container<Base>& cont)
{
    Derived2 obj;
    cont.push_back(obj);
}

Container<Derived> c;
foo(c);
以下是一个(可能的)完整示例:

#include <memory>
#include <vector>
#include <iostream>

template<typename T>
using Container = std::vector<T>;

struct Base { virtual ~Base() { } };
struct Derived : Base { };

int main()
{
    Container<std::shared_ptr<Base>> cb;
    cb.push_back(std::make_shared<Derived>());
    cb.push_back(std::make_shared<Base>());
    cb.push_back(std::make_shared<Derived>());

    Container<std::shared_ptr<Derived>> cd;
    for (auto &pB : cb)
    {
        std::shared_ptr<Derived> pD = std::dynamic_pointer_cast<Derived>(pB);
        if (pD != nullptr)
        {
            cd.push_back(pD);
        }
    }

    std::cout << cd.size(); // Prints 2
}
#包括
#包括
#包括
模板
使用Container=std::vector;
结构Base{virtual~Base(){};
派生结构:基{};
int main()
{
集装箱cb;
cb.push_back(std::make_shared());
cb.push_back(std::make_shared());
cb.push_back(std::make_shared());
集装箱光盘;
用于(自动和pB:cb)
{
std::shared_ptr pD=std::dynamic_pointer_cast(pB);
如果(pD!=空PTR)
{
cd.推回(pD);
}
}

std::cout注意,要进行多态操作,您需要使用指针或引用。您的容器可能会这样做,但可能不会——您还没有告诉我们

如果您有<代码>容器,您可以同时保存基础和派生对象(尽管您可能需要考虑制作<<代码>容器 或类似)。

如果您有一个
容器
(或
容器
),您可以创建一个指向这些元素的并行
容器
,但要注意它们不同步


另一个选项是根据需要逐个强制转换元素。

如果所有内容都是从一个非模板抽象接口类派生的,该接口类包含所有需要的方法,那么vTable将自动执行强制转换:-D。然后,您将需要该接口类指针的容器。我使用复合模式完成了这一操作,可以正常工作就像一个charme!

@ShafikYaghmour当然是。OP想要将
容器
转换为需要滑动的
容器
。你的容器是什么,你想做什么?另一种方法是创建一个包装容器或容器的类,并具有读取基*对象的函数。你不能使用动态或静态c强制转换,因为模板类的实例化之间没有关系。嗯,我想是的,但我不确定,因为模板。但是如果他派生接口类的容器,会有关系吗?我最好删除我的答案。
#include <memory>
#include <vector>
#include <iostream>

template<typename T>
using Container = std::vector<T>;

struct Base { virtual ~Base() { } };
struct Derived : Base { };

int main()
{
    Container<std::shared_ptr<Base>> cb;
    cb.push_back(std::make_shared<Derived>());
    cb.push_back(std::make_shared<Base>());
    cb.push_back(std::make_shared<Derived>());

    Container<std::shared_ptr<Derived>> cd;
    for (auto &pB : cb)
    {
        std::shared_ptr<Derived> pD = std::dynamic_pointer_cast<Derived>(pB);
        if (pD != nullptr)
        {
            cd.push_back(pD);
        }
    }

    std::cout << cd.size(); // Prints 2
}