这种C++语法代表什么意思?该如何解决
这种C++语法代表什么意思?
有些问题寻求解答:
假设有定义语句 : CefStructBase<CefMouseEventTraits >:: struct_type xx;
1、以下代码在Struct/Class中写typedef来定义一个新类型的语法称为什么?(我网上搜索“struct中使用typedef”,没有找到任何结果。。。)
2、模板类中的头部的 :public traits::struct_type 代表什么意思?究竟是继承traits还是继承traits里新定义的struct_type类型????
3、模板类中的typedef typename traits::struct_type struct_type; 为什么要写typename?去掉不行吗?
4、模板类中为什么可以使用Init();clear();set();方法? 它是在什么时候继承的CefMouseEventTraits???
结构:
模板类:
------解决思路----------------------
很多时候,在设计模板类时,是不知道模板参数的具体信息的。通过在(作为模板参数的)类中声明typedef提供一些信息给模板类是十分必要的。这种技巧叫做type traits。C++11就有一个<type_traits>的标准头,提供用于判断一个类型的特性的辅助类。
在这里,CefStructBase并不知道模板参数traits的相关信息,但假定traits有一个typedef以便于它实例化与之相关的类成员
struct_type* attached_to_;
在CefStructBase中再用typedef是为了简化代码,否则必须写成
typename traits::struct_type * attached_to_;
当然,也可能是再向其他模板类暴露其类型的相关信息
有些问题寻求解答:
假设有定义语句 : CefStructBase<CefMouseEventTraits >:: struct_type xx;
1、以下代码在Struct/Class中写typedef来定义一个新类型的语法称为什么?(我网上搜索“struct中使用typedef”,没有找到任何结果。。。)
2、模板类中的头部的 :public traits::struct_type 代表什么意思?究竟是继承traits还是继承traits里新定义的struct_type类型????
3、模板类中的typedef typename traits::struct_type struct_type; 为什么要写typename?去掉不行吗?
4、模板类中为什么可以使用Init();clear();set();方法? 它是在什么时候继承的CefMouseEventTraits???
结构:
struct CefMouseEventTraits {
typedef cef_mouse_event_t struct_type;
static inline void init(struct_type* s) {}
static inline void clear(struct_type* s) {}
static inline void set(const struct_type* src, struct_type* target,
bool copy) {
target->x = src->x;
target->y = src->y;
target->modifiers = src->modifiers;
}
};
模板类:
template <class traits>
class CefStructBase : public traits::struct_type {
public:
typedef typename traits::struct_type struct_type;
CefStructBase() : attached_to_(NULL) {
Init();
}
virtual ~CefStructBase() {
// Only clear this object's data if it isn't currently attached to a
// structure.
if (!attached_to_)
Clear(this);
}
CefStructBase(const CefStructBase& r) {
Init();
*this = r;
}
CefStructBase(const struct_type& r) { // NOLINT(runtime/explicit)
Init();
*this = r;
}
///
// Clear this object's values.
///
void Reset() {
Clear(this);
Init();
}
///
// Attach to the source structure's existing values. DetachTo() can be called
// to insert the values back into the existing structure.
///
void AttachTo(struct_type& source) {
// Only clear this object's data if it isn't currently attached to a
// structure.
if (!attached_to_)
Clear(this);
// This object is now attached to the new structure.
attached_to_ = &source;
// Transfer ownership of the values from the source structure.
memcpy(static_cast<struct_type*>(this), &source, sizeof(struct_type));
}
///
// Relinquish ownership of values to the target structure.
///
void DetachTo(struct_type& target) {
if (attached_to_ != &target) {
// Clear the target structure's values only if we are not currently
// attached to that structure.
Clear(&target);
}
// Transfer ownership of the values to the target structure.
memcpy(&target, static_cast<struct_type*>(this), sizeof(struct_type));
// Remove the references from this object.
Init();
}
///
// Set this object's values. If |copy| is true the source structure's values
// will be copied instead of referenced.
///
void Set(const struct_type& source, bool copy) {
traits::set(&source, this, copy);
}
CefStructBase& operator=(const CefStructBase& s) {
return operator=(static_cast<const struct_type&>(s));
}
CefStructBase& operator=(const struct_type& s) {
Set(s, true);
return *this;
}
protected:
void Init() {
memset(static_cast<struct_type*>(this), 0, sizeof(struct_type));
attached_to_ = NULL;
traits::init(this);
}
static void Clear(struct_type* s) { traits::clear(s); }
struct_type* attached_to_;
};
------解决思路----------------------
很多时候,在设计模板类时,是不知道模板参数的具体信息的。通过在(作为模板参数的)类中声明typedef提供一些信息给模板类是十分必要的。这种技巧叫做type traits。C++11就有一个<type_traits>的标准头,提供用于判断一个类型的特性的辅助类。
在这里,CefStructBase并不知道模板参数traits的相关信息,但假定traits有一个typedef以便于它实例化与之相关的类成员
struct_type* attached_to_;
在CefStructBase中再用typedef是为了简化代码,否则必须写成
typename traits::struct_type * attached_to_;
当然,也可能是再向其他模板类暴露其类型的相关信息