A pair holds instances of types. An abstract type cannot be instantiated. Thus, an abstract type cannot be in a pair.
Eh ?? That would make a map containing base class pointers completely useless.
Code:
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <map>
#include <iostream>
#include <conio.h>
class IFoo
{
public:
virtual ~IFoo() { }
virtual void PrintOut() = 0;
};
class Foo : public IFoo
{
public:
Foo() { }
virtual ~Foo() { }
virtual void PrintOut()
{
std::cout << "Hello from Foo" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef std::map<unsigned int, IFoo *> FooMap;
FooMap testMap;
std::pair<unsigned int,IFoo *> FooIntPair;
FooIntPair.first = 0;
FooIntPair.second = new Foo();
testMap.insert(FooIntPair);
FooMap::iterator iter;
FooMap::iterator end(testMap.end());
iter = testMap.find(0);
if (iter != end)
{
Foo *pFoo = dynamic_cast<Foo *>(iter->second);
pFoo->PrintOut();
}
iter = testMap.begin();
while (iter != end)
{
delete iter->second;
iter->second = 0;
++iter;
}
testMap.clear();
_getch();
return 0;
}
Technically dynamic_cast is not needed and static_cast will do since we know the object with key 0 is indeed a Foo.
Another example using IFoo * as the key. Not sure why you would want to map one IFoo to another IFoo, but it is possible.
Code:
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <map>
#include <iostream>
#include <conio.h>
class IFoo
{
public:
virtual ~IFoo() { }
virtual void PrintOut() = 0;
};
class Foo : public IFoo
{
public:
Foo() { }
virtual ~Foo() { }
virtual void PrintOut()
{
std::cout << "Hello from Foo" << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef std::map<IFoo *, IFoo *> FooMap;
FooMap testMap;
std::pair<IFoo *,IFoo *> FooPair;
Foo *pFooOne = new Foo();
Foo *pFooTwo = new Foo();
FooPair.first = pFooOne;
FooPair.second = pFooTwo;
testMap.insert(FooPair);
FooMap::iterator iter;
FooMap::iterator end(testMap.end());
iter = testMap.find(pFooOne);
if (iter != end)
{
Foo *pFoo = dynamic_cast<Foo *>(iter->second);
pFoo->PrintOut();
pFoo = dynamic_cast<Foo *>(iter->first);
pFoo->PrintOut();
}
iter = testMap.begin();
while (iter != end)
{
delete iter->first;
delete iter->second;
iter->second = 0;
++iter;
}
testMap.clear();
_getch();
return 0;
}
std::pair is just a template that allows you to specify the data types for first and second. You can put any data type in them. You could not do std::pair<IFoo,IFoo> b/c IFoo is abstract but you can certainly do std::pair<IFoo *,IFoo *> without ever creating an impl for IFoo. You will not be able to have first or second ever point at anything meaningful unless you create an impl for IFoo.
I believe what brewbuck meant was that if you declare std::pair in a way that first or second are instances of objects then you must have instances of their type to use them in the pair. In this case you cannot have an instance b/c first or second is abstract. Keep in mind the pair declaration will always work provided the types exist but usage depends on the types you specified in the declaration.
IE:
std::pair<std::string,unsigned int> stringIntPair;