const K& k = K() 在这个构造函数中是什么意思?
What does const K& k = K() mean in this constructor function?
我很难理解此代码片段的语法
" const k& k = k(),const v& v = v()"的目的是什么?
template<typename K, typename V>
class Entry{
public:
typedef K Key;
typedef V Value;
public:
Entry(const K& k = K(), const V& v = V()) : _key(k), _value(v) {}
....
....
....
private:
K _key;
V _value;
};
谢谢
K
和 V
是模板参数,它们可以是任何数据类型Entry
想要的用户。
k
和v
是Entry
构造函数的输入参数。它们通过const引用传递,并指定了默认值,其中 K()
默认构建类型K
的临时对象,而 V()
默认值default-contructtruct oftem V
的临时对象。如果用户在创建Entry
对象实例时不提供明确的输入值,则使用默认值。
例如:
Entry<int, int> e1(1, 2);
// K = int, k = 1
// V = int, v = 2
Entry<int, int> e2(1);
// aka Entry<int, int> e2(1, int())
// K = int, k = 1
// V = int, v = 0
Entry<int, int> e3;
// aka Entry<int, int> e3(int(), int())
// K = int, k = 0
// V = int, v = 0
Entry<std::string, std::string> e4("key", "value");
// K = std::string, k = "key"
// V = std::string, v = "value"
Entry<std::string, std::string> e5("key");
// aka Entry<std::string, std::string> e4("key", std::string())
// K = std::string, k = "key"
// V = std::string, v = ""
Entry<std::string, std::string> e6;
// aka Entry<std::string, std::string> e4(std::string(), std::string())
// K = std::string, k = ""
// V = std::string, v = ""
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数后面的宏.什么意思?
- 子类的构造函数后跟冒号后的基类构造函数是什么意思?
- const K& k = K() 在这个构造函数中是什么意思?
- 构造函数中的"class"是什么意思?
- 在构造函数 foo() 中有一个模板是什么意思 <T>C++?
- C++:这种构造函数格式是什么意思
- 默认值的参数化构造函数是什么意思
- C++错误"无效的构造函数;您的意思可能是"帐户(常量帐户&)
- 使用 g++ 编译C++时,"隐藏构造函数"警告是什么意思?
- 构造函数中指针的默认值是什么意思?
- c++,构造函数后面的冒号是什么意思
- 将基类构造函数强制转换为派生类构造函数是什么意思?
- Tensorflow - ops 构造函数是什么意思