有或没有用C++包装的类
With or without class wrappered in C++
我有一个基本问题,因为我已经有一段时间没有使用C++了。
我有一个这样的头文件:
它将保持不变,但是cpp文件将更改
#ifndef DOG_H_
#define DOG_H_
class Dog : Animal {
private:
std::string breed;
public:
Dog(std::string name, int age, std::string);
};
#endif /* DOG_H_ */
然后是CPP版本1:
#include "Dog.h"
Dog::Dog(std::string name, int age, std::string breedIn){
Animal(name, age);
breed = breedIn;
}
或CPP版本2:
#include "Dog.h"
class Dog{
Dog::Dog(std::string name, int age, std::string breedIn){
Animal(name, age);
breed = breedIn; // the var name breed does not resolve
}
};
版本1和版本2之间的区别在于,第二个版本被封装在class
定义中。
为什么我要做一个而不做另一个。
其次,在第二个版本中,变量名bread没有解析。为什么?
Q:在第二个版本中,变量名bread不解析。为什么?
A: 因为这是错误的。
您声明您的类一次,例如在标头中。
您可以在内联(在声明类时,在标头本身中)或在单独的.cpp中(如示例1中所做的那样)定义方法实现。这些链接可能有助于进一步解释:
-
http://www.cplusplus.com/doc/tutorial/classes/
-
http://www.cprogramming.com/declare_vs_define.html
相关文章:
- 如何在c++17中制作一个模板包装器/装饰器
- std::vector的包装器,使数组的结构看起来像结构的数组
- 如何在c++迭代器类型中包装std::chrono
- 是否可以用"iostream"包装现有的TCP/OOpenSSL会话
- 用pybind11包装C++抽象类时出错
- 为左值和右值的包装器实现C++范围
- C结构,其指针将被包装在unique_ptr中
- 如何包装第三方DLL在R中使用
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 将 N-arg 函数包装到另一个函数中
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 包装一个对象并假装它是一个 int
- 使用 Python Extension API 包装复杂C++类
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 包装C++类时不完整的类型 GLFWwindow
- 将函数包装器转换为 std::function
- C++函数包装器来捕获某些信号
- 创建包装升压适配器的自定义范围类
- 如何包装(撰写)增强 hana 地图并访问括号运算符(运算符 [])?
- 使用运算符*重载包装矩阵乘法