c++中与这个类定义等价的是什么?
What is the c++ equivalent of this class definition
我正试图学习c++,但我的第一语言是Python。我正在努力理解c++中的构造函数,更具体地说,是可变大小的数组和字符串。有没有人能写出下面类定义的c++等效代码,这样我就可以遵循这个逻辑了?
class Fruit(object):
def __init__(self, name, color, flavor, poisonous):
self.name = name
self.color = color
self.flavor = flavor
self.poisonous = poisonous
class Fruit {
std::string name;
std::tuple<uint8_t, uint8_t, uint8_t> color; // for RGB colors
std::string flavor; // Assuming flavor is a string
bool poisonous;
Fruit(const std::string& nm, const std::tuple<uint8_t, uint8_t, uint8_t>& clr, const std::string& flvr, const bool psns) : name(nm), color(clr), flavor(flvr), poisonous(psns) {}
}
__init__
函数的作用与c++中的构造函数非常相似。因为在c++中,您需要指定变量类型,我采取了一些自由假设name
和flavor
是字符串,color
是一个值从0到255 (RGB)的3元组,poisonous
是一个布尔值(bool
)。
相关文章:
- C++中"dependent name"的定义是什么?
- 什么是自定义比较器以及如何在 C++ 的排序函数中使用它?
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 在模板类之外定义友元函数的正确方法是什么?
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 自定义数据结构的优点是什么?
- [未定义提及'SetLastError@4']是什么意思?
- 在C++中,创建'n'数量的对象的推荐方法是什么,其中n是用户定义的。我该怎么做?
- 此模板定义在 C++ 中是什么意思?
- 根据 c++ 标准在该宏定义中推送/弹出宏时的行为是什么
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 这种错误的原因是什么:将"功能"重新定义为不同类型的符号
- 朋友定义函数的名称空间是什么
- 对struct::struct的未定义引用是什么意思
- c++ 17 中结构自动定义构造函数的规则是什么?
- 在 .text 部分中定义只读数据的原因是什么?
- 将字段(在类中)定义为引用的缺点是什么?
- (.text+0x20):对"主要"的未定义引用是什么意思
- Arduino IDE中自定义库类的.h文件编译错误的原因是什么