gpt4 book ai didi

c++ - 命名空间标识符是否应该放在每个参数的前面?

转载 作者:太空狗 更新时间:2023-10-29 21:22:54 25 4
gpt4 key购买 nike

例如,

矩阵.h

namespace Matrix
{
class mat
{
public:
mat(int row, int col);
const mat &operator=(const mat &rhs);
}
}

矩阵.cpp

Matrix::mat::mat(int row, int col)
{ // implementation here }

const Matrix::mat &Matrix::mat::operator=(const mat &rhs)
{ // implementation here }

以上代码编译没有任何问题。问题是,我是否应该将 namespace 标识符放在参数前面,例如 const mat operator=(const Matrix::mat &rhs);
const Matrix::mat Matrix::mat::operator=(const Matrix::mat &rhs)?执行此操作的约定方式是什么?为什么它会在不添加标识符的情况下编译?

最佳答案

只需在命名空间中定义您的代码

矩阵.cpp

namespace Matrix {

mat::mat(int row, int col)
{ // implementation here }

mat& mat::operator=(const mat &rhs)
{ // implementation here }

} //namespace Matrix

关于c++ - 命名空间标识符是否应该放在每个参数的前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613821/

25 4 0