不确定如何使用我的类和命名空间创建对象?c++
Not Sure how to create object with my class and namespace? c++
我已经编码了一个类大片和命名空间cs52。创建对象的正确方法是吗
1) cs52::大片b(100,《兰博》,《史泰龙》);
2.)std::大片b(100,《兰博》,《史泰龙》);
还是其他什么?
代码:
#include <string>
using namespace std;
namespace cs52 {
class Blockbuster{
public:
Blockbuster( int length, string title, string star );
friend bool equal( const Blockbuster & b1, const Blockbuster & b2 );
int getLength();
string getTitle();
string getPlace();
void setTitle( string title );
void setPlace( string place );
void setLength( int length );
private:
int my_Length; // feature length in minutes
string my_Title; // feature title
string my_Star; // leading actor or actress
};
}
此外,我实际上还没有定义我的c++文件函数。将实现为boolBlockbuster::相等(const Blockbuster&b1,const Blockbuster&b2)
还是我需要在声明中加入朋友?
using namespace std
不受欢迎(被认为是不良做法),因为它会导致冲突。我建议你去掉那条线。要实例化对象,请使用cs52::Blockbuster
。您永远不会在编写的类上使用std::
,因为这是标准库的命名空间。您可能还想添加一些更具体的命名空间指令,如using std::string
。请注意,这是我自己的做事方式,当涉及到C++和名称空间时,每个人都有不同的意见。例如,在头文件中声明名称空间有一些危险,因为它可能会意外地更改包含名称空间的代码的含义
回答您的第二个问题:
您只能在声明中使用friend
关键字,而不能在定义/实现中使用。(您将函数声明为友元函数)。
相关文章:
- 如何在命名空间内创建类?
- 如何创建函数原型命名空间
- 命名空间:不能在没有对象的情况下调用成员函数
- 命名空间、标头和对象的层次结构
- 由于命名空间而导致的对象实例化错误
- 有没有一种方法可以创建具有广义属性的命名空间
- C++将类添加到命名空间以创建新的类对象
- 其他命名空间中的对象没有包含,否则文件相同
- 我是否可以在类似于 C# 的 C++ 中创建全局命名空间层次结构,以帮助开发人员使用我们的代码?
- 模板对象的模板友元函数和命名空间
- 不确定如何使用我的类和命名空间创建对象?c++
- C++:有没有办法创建模板化命名空间
- 不是创建模板类后的类、命名空间或枚举
- 命名空间是否可以用作C++中的对象模板
- C++:如何让函数接受来自任何命名空间的具有相同类名的对象
- C++访问具有相同名称的变量/对象的命名空间中的全局变量/对象
- 无法从另一个命名空间创建类的实例
- 从命名空间返回对象是否安全
- 使用命名空间创建c++库
- 如何在 c++ 中创建在命名空间内定义的类对象