【发布时间】:2013-11-30 21:41:42
【问题描述】:
我编写了一个类大片和命名空间 cs52。创建对象的正确方法是
1) cs52::Blockbuster b(100, “兰博”, “西尔维斯特史泰龙”);
2.)std::Blockbuster b(100, "Rambo", "Sylvester Stallone");
还是有什么不同?
代码:
#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++ 文件函数。将被实现为 bool Blockbuster::equal(const Blockbuster & b1, const Blockbuster & b2)
或者我需要在声明中的某处包含朋友吗?
【问题讨论】:
-
删除
using namespace std并使用cs52::Blockbuster -
当您尝试 1) 和 2) 时发生了什么?
-
我实际上还没有定义我的 c++ 文件函数。将实现为 bool Blockbuster::equal(const Blockbuster & b1, const Blockbuster & b2) 还是我需要在声明中的某处包含朋友?
-
在标题中使用命名空间会污染客户端的命名空间。
标签: c++ class object namespaces