将 typedef-ed class 加为好友 (c++)
Friending a typedef-ed class (c++)
我正在经历一些危机,我需要将类型定义的模板 class 加为好友,但我收到大量错误(太多无法粘贴到此处)。我已经放了 MRE here 这样你们就不必设置文件了。
为了完整起见,以下是 .h 文件:
//Keyboard.h
#pragma once
namespace wm
{
namespace io
{
class Keyboard
{
//friend wm::MessageHandler;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//Mouse.h
#pragma once
namespace wm
{
namespace io
{
class Mouse
{
//friend wm::MessageHandler;
//friend class wm::MessageHandler; ??
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//MessageHandler.h
#pragma once
#include "Keyboard.h"
#include "Mouse.h"
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler
{
public:
static _MessageHandler& Instance();
_MessageHandler(const _MessageHandler&) = delete;
void operator=(const _MessageHandler&) = delete;
private:
_MessageHandler() = default;
static void SomeMsgHandelingFunc(); //calls SomeServerFunc from window and mouse
};
typedef _MessageHandler<io::Keyboard, io::Mouse> MessageHandler;
}
事实上,上面的代码可以正常编译和运行。但我想将键盘和鼠标中的 SomeServerFunc
功能设为私有 class,并使 MessageHandler
成为 Keyboard
和 Mouse
的朋友。我尝试了不同的方法,但到目前为止没有任何效果。
我正在做 friend class wm::MessageHandler;
但我得到详细类型指的是 typedef 错误。
然后我发现在 C++ 11 之后,您可以执行 friend wm::MessageHandler;
,这会产生大量错误。我也试过了
friend wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend class wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend wm::_MessageHandler<Keyboard, Mouse>
;
friend class wm::_MessageHandler<Keyboard, Mouse>
;
运气不好。
非常感谢任何帮助。
_MessageHandler
不是 class 而是模板。试试看:
namespace wm
{
template<class Keyboard, class Mouse> class MessageHandler;
namespace io
{
class Keyboard
{
template<class Keyboard, class Mouse> friend class wm::MessageHandler;
...
};
}
}
to make the SomeServerFunc function in both keyboard and mouse classes private and make MessageHandler a friend of Keyboard and Mouse.
我将class声明添加到mouse.h和keyboard.h,使编译器happy.Here是代码:
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Keyboard;
class Mouse
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Mouse;
class Keyboard
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
我正在经历一些危机,我需要将类型定义的模板 class 加为好友,但我收到大量错误(太多无法粘贴到此处)。我已经放了 MRE here 这样你们就不必设置文件了。
为了完整起见,以下是 .h 文件:
//Keyboard.h
#pragma once
namespace wm
{
namespace io
{
class Keyboard
{
//friend wm::MessageHandler;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//Mouse.h
#pragma once
namespace wm
{
namespace io
{
class Mouse
{
//friend wm::MessageHandler;
//friend class wm::MessageHandler; ??
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
//MessageHandler.h
#pragma once
#include "Keyboard.h"
#include "Mouse.h"
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler
{
public:
static _MessageHandler& Instance();
_MessageHandler(const _MessageHandler&) = delete;
void operator=(const _MessageHandler&) = delete;
private:
_MessageHandler() = default;
static void SomeMsgHandelingFunc(); //calls SomeServerFunc from window and mouse
};
typedef _MessageHandler<io::Keyboard, io::Mouse> MessageHandler;
}
事实上,上面的代码可以正常编译和运行。但我想将键盘和鼠标中的 SomeServerFunc
功能设为私有 class,并使 MessageHandler
成为 Keyboard
和 Mouse
的朋友。我尝试了不同的方法,但到目前为止没有任何效果。
我正在做 friend class wm::MessageHandler;
但我得到详细类型指的是 typedef 错误。
然后我发现在 C++ 11 之后,您可以执行 friend wm::MessageHandler;
,这会产生大量错误。我也试过了
friend wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend class wm::_MessageHandler<io::Keyboard, io::Mouse>
;
friend wm::_MessageHandler<Keyboard, Mouse>
;
friend class wm::_MessageHandler<Keyboard, Mouse>
;
运气不好。
非常感谢任何帮助。
_MessageHandler
不是 class 而是模板。试试看:
namespace wm
{
template<class Keyboard, class Mouse> class MessageHandler;
namespace io
{
class Keyboard
{
template<class Keyboard, class Mouse> friend class wm::MessageHandler;
...
};
}
}
to make the SomeServerFunc function in both keyboard and mouse classes private and make MessageHandler a friend of Keyboard and Mouse.
我将class声明添加到mouse.h和keyboard.h,使编译器happy.Here是代码:
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Keyboard;
class Mouse
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Mouse(const Mouse&) = delete;
void operator=(const Mouse&) = delete;
Mouse() = default;
public:
static Mouse& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}
#pragma once
namespace wm
{
template < class Keyboard, class Mouse >
class _MessageHandler;
namespace io
{
class Mouse;
class Keyboard
{
friend class _MessageHandler<io::Keyboard, io::Mouse>;
private:
Keyboard(const Keyboard&) = delete;
void operator=(const Keyboard&) = delete;
Keyboard() = default;
public:
static Keyboard& Instance();
private:
static void SomeServerFunc();
public:
static void SomeClientFunc();
};
}
}