我寫了一個抽象類foo
,並且bar
類從foo繼承。map <string,pair <string,foo * >>和map <string,pair <string,foo&>>有什麼區別?
我想創建一個地圖容器,它是map<string, pair<string, foo&>>
,但我無法成功編譯。編譯器告訴我
「std::pair<std::string,foo &>::pair」: not appropriate default constructor
這裏是代碼:
#include <iostream>
#include <string>
#include <windows.h>
#include <map>
#include <utility>
using namespace std;
class foo
{
public:
virtual void t() = 0;
};
class bar :public foo
{
public:
void t()
{
cout << "bar" << endl;
}
};
int main()
{
bar b;
//wrong
//map<string, pair<string, foo&>> t;
//pair<string, foo&> p("b", b);
//t["t"] = p;
//right
map<string, pair<string, foo*>> t;
pair<string, foo*> p("b", &b);
t["t"] = p;
p.second->t();
}
我想知道map<string, pair<string, foo*>>
和map<string, pair<string, foo&>>
之間的差異。
你知道指針和引用之間有什麼不同嗎? – NathanOliver
https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in –
@FrançoisAndrieux您能否詳細解釋一下原因? – lens