bodacydo
bodacydo

Reputation: 79469

Can I use CComPtr inside of a std::map?

I'm writing a program in Windows COM in C++ and I'm using CComPtr for smart pointers.

The question I can't find answer to is - Can I use CComPtr inside of a std::map.

I have the following code fragment (simplified):

std::map<int, CComPtr<IErrorInfo> > ErrorMap;

I wish to maintain this mapping between ints and IErrorInfo error infos.

However I'm not sure if I can do the following:

CComPtr<IErrorInfo> result;
GetErrorInfo(0, &pErrInfo);

ErrorMap.insert(std::make_pair(0, result));

I'm concerned about ownership of the result smart pointer and if it will get released correctly when ErrorMap gets destroyed?

Upvotes: 4

Views: 968

Answers (1)

Steve Townsend
Steve Townsend

Reputation: 54178

You need to wrap your CComPtr in CAdapt for this to work.

The adapter class CAdapt is useful because many container classes (such as the STL container classes) expect to be able to obtain the addresses of their contained objects using the address-of operator. The redefinition of the address-of operator can confound this requirement, typically causing compilation errors and preventing the use of the unadapted type with that container. CAdapt provides a way around those problems.

Upvotes: 5

Related Questions