Guest User

Untitled

a guest
Oct 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. template <typename T>
  2. class XPtr
  3. {
  4. private:
  5. T* p;
  6. public:
  7. XPtr(T* ptr) {
  8. p = ptr;
  9. }
  10.  
  11. XPtr(const XPtr& ptr) {
  12. p = ptr.p;
  13. }
  14.  
  15. T* operator&() {
  16. return p;
  17. }
  18.  
  19. // The following declaration compiles but still
  20. // is it the correct way of declaring friend function?
  21. template<typename T>
  22. friend std::wstring getString(const XPtr<T>& p);
  23. };
  24.  
  25. template<typename T>
  26. std::wstring getString(const XPtr<T>& p) {
  27. std::wstringstream ss;
  28. ss << L"{" << p.p;
  29. if (p.p != 0) { ss << L"," << *p.p; }
  30. ss << L"}";
  31. return ss.str();
  32. }
  33.  
  34. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  35.  
  36. namespace Microsoft {
  37. namespace VisualStudio {
  38. namespace CppUnitTestFramework {
  39. //Actually I would like to specialize for XPtr<T> instead of XPtr<int>,
  40. //how to do it?
  41. template<>
  42. static std::wstring ToString<XPtr<int> >(const class XPtr<int> & t)
  43. { return getString<int>(t); }
  44. }
  45. }
  46. }
  47.  
  48. namespace TestRCPtr1
  49. {
  50. TEST_CLASS(UnitTest1)
  51. {
  52. public:
  53.  
  54. TEST_METHOD(TestMethod6)
  55. {
  56. XPtr<int> pint1(new int(1));
  57. XPtr<int> pint2(pint1);
  58. Assert::AreEqual(&pint1, &pint2); // Test passes
  59. Assert::AreSame(pint1, pint2); // Test fails, see below for error message
  60. }
  61. };
  62. }
Add Comment
Please, Sign In to add comment