相关疑难解决方法(0)

C/C++ Scope在两个不同的.cpp文件中

我想知道为什么你不能在2个不同的.cpp文件中声明一个具有相同名称的全局.我的理解是考虑范围,它应该只对特定的.cpp文件可见,而不是其他地方,但它显然是在抱怨.我这样做的原因是代码中的通用性,就是这样.有任何想法吗?

编辑清晰度

a.cpp

int g_x;

b.cpp

int g_x;

c c++

5
推荐指数
2
解决办法
3010
查看次数

辅助工具的静态自由函数或成员函数?

假设您具有以下非静态成员函数:

// .h
struct C {
    void f();
}
Run Code Online (Sandbox Code Playgroud)

现在假设您希望C::f通过使用一些特定的子函数来实现C::f,以使其更短且更易读; 例如:

// .cpp
void C::f() {
    h1();
    h2();
    //...
    hn();
}
Run Code Online (Sandbox Code Playgroud)

假设许多h()函数不需要访问任何数据成员.这意味着您可以将函数定义为静态自由函数(在一个.cpp中)或作为成员函数(静态或非静态).

你会让它们成为静态自由函数或C的函数成员吗?

第一种情况的一个优点是您不必在以下情况下声明它们C:

// .cpp
static void h1() {//...}
static void h2() {//...}
static void hn() {//...}
Run Code Online (Sandbox Code Playgroud)

此外,如果我没有错,则不存在全局命名空间污染的风险,因为它们是静态的,即它们只能从同一单元.cpp内的其他函数中看到(其中C :: f也被定义).

而在第二种情况下,你将不得不声明它们C,虽然,正如我所说,它们只应该被使用C::f.

// .h
struct C {
    void f();
  private:
    static void h1(); // can be either static or non-static
    static void h2();
    static void hn();
}

// …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
1502
查看次数

非类功能可以私有化吗?

我在我创建的命名空间中有一些函数在我的程序中使用.

在头文件中:

namespace NQueens
{
    static int heur = 0;
    int CalcHeuristic(char** state, int size);
    void CalcHorzH(char ** state, int &heuristic, int size);
    void CalcColH(char ** state, int &heuristic, int size);
    void CalcDiagH(char ** state, int &heuristic, int size);
    int calcCollisions(int queensPerRow, int size);
}
Run Code Online (Sandbox Code Playgroud)

一切正常.但是,从我的外部程序代码实际调用的唯一函数是CalcHeuristic(char** state, int size)函数.然后该函数调用其他函数本身.

由于这些不属于某个类,我的编译器不会让我声明其他函数private.有没有办法做到这一点?我应该担心吗?

c++

5
推荐指数
2
解决办法
1245
查看次数

我应该使用静态还是命名空间?

我有一个专用的HW寄存器头文件,我创建了一个名称空间,就像这样,它保存了我所有的HW寄存器地址:

namespace{
 const uint32_t Register1                    = (0x00000000);
 const uint32_t Register2                    = (0x00000004);
 const uint32_t Register3                    = (0x00000008);
 const uint32_t Register4                    = (0x0000000c);
}
Run Code Online (Sandbox Code Playgroud)

这被认为比使用更好:

 static const uint32_t Register1             = (0x00000000);
 static const uint32_t Register2             = (0x00000004);
 static const uint32_t Register3             = (0x00000008);
 static const uint32_t Register4             = (0x0000000c); 
Run Code Online (Sandbox Code Playgroud)

我想命名空间的意思是我们不会污染全局命名空间.是对的吗?

我有一个.cpp,它使用头文件.

c++ static namespaces

3
推荐指数
1
解决办法
86
查看次数

最佳实践 - 应该在哪里定义类的 const 数据

我编写一个定义和使用的类const string (此字符串定义路径或路径的一部分)我应该在哪里定义它:在 cpp 或 h 中?它应该是课程的一部分吗?目前,此数据仅在内部成员函数内部使用。哪个选项更好?

myClass.h
    // const std::string PATH = "Common\\Data\\input.txt" (1)
    class MyClass
    {
      public:
        // const std::string m_path = "Common\\Data\\input.txt" (2)
      private:
        // const std::string m_path = "Common\\Data\\input.txt" (3)
    }
myClass.cpp
// const std::string PATH = "Common\\Data\\input.txt"(4)
Run Code Online (Sandbox Code Playgroud)

c++ constants

3
推荐指数
1
解决办法
702
查看次数

标签 统计

c++ ×5

c ×1

constants ×1

namespaces ×1

static ×1