如何在 C++ 中为变量创建全局常量别名

How to make global constant aliases to variables in C++

我是 C++ 的新手,对于任何严重的错误深表歉意。

问题陈述

我的代码是做数学计算的,所以main.cpp/main.h做设置,读入参数等等,然后单独一个file/header,叫它driver.cpp/driver.h,进行计算。

为了封装参数,我制作了几个用户定义的数据类型,在 main.cpp 中初始化它们,并将它们传递给在 driver.cpp 中定义的函数。我希望这些参数被视为 driver.cpp 中函数的常量,并且我还想为它们起别名以提高可读性。最好给它们起别名 once 而不是在每个函数中。这可能吗?

我试图做一个简单的例子来说明我想要什么,尽管它不会运行因为你不能使用常量引用我在下面做的方式。

我想要的想法:

main.cpp

struct myStruct_t{
    int a,b,c;
};

int main(int argc,char **argv){
    myStruct_t myStruct;
    myStruct.a=1;
    myStruct.b=2;
    myStruct.c=3;

    driver(myStruct);
}

driver.cpp

const int &a,&b,&c;
void func1();
void func2();

driver(const myStruct_t& myStruct){
    a = myStruct.a;
    b = myStruct.b;
    c = myStruct.c;     
    func1();
    func2();  
} 
void func1(){
    // do stuff with a,b,c
}
void func2(){
    // do stuff with a,b,c
}

另一方面,它可以按如下方式实现驱动程序。我不太喜欢它,因为我需要在每个函数中复制引用声明。

什么有效但我不太喜欢:

alt_driver.cpp

void func1(const myStruct_t& myStruct);
void func2(const myStruct_t& myStruct);

driver(const myStruct_t& myStruct){ 
    func1(myStruct);
    func2(myStruct);  
}

void func1(const myStruct_t& myStruct){
    const int& a = myStruct.a;
    const int& b = myStruct.b;
    const int& c = myStruct.c;
    // do stuff with a,b,c
}

void func2(const myStruct_t& myStruct){
    const int& a = myStruct.a;
    const int& b = myStruct.b;
    const int& c = myStruct.c;
    // do stuff with a,b,c
}

除非性能是一个因素,否则我建议不要依赖对变量的全局引用。我建议使用功能界面访问它们。

// Create a namespace for driver.cpp 
// Put all the helper functions and data in the namespace.
namespace driver_ns
{
   myStruct_t const* myStructPtr = nullptr;

   int const& a()
   {
      return myStructPtr->a;
   }

   int const& b()
   {
      return myStructPtr->b;
   }

   int const& c()
   {
      return myStructPtr->c;
   }
}

using namesapce driver_ns;

void func1();
void func2();

driver(const myStruct_t& myStruct){
   myStructPtr = &myStruct;
    func1();
    func2();  
} 

void func1(){
    // do stuff with a,b,c, usig a(), b(), and c()
}
void func2(){
    // do stuff with a,b,c, usig a(), b(), and c()
}

如果需要在多个文件中访问abc,在一个共享的.h文件中添加功能接口,在一个独立的文件中实现使用它们的文件。

如果可以使用指向 const 的指针而不是 const 引用,那么类似下面的内容可能会起作用。 (对于一个实用的解决方案,我冒昧地将公共声明分离到一个头文件中,driver.h。这是标准的 C++ 实践。)

driver.h

#ifndef DRIVER_H
#define DRIVER_H

struct myStruct_t{
    int a,b,c;
};
void driver(const myStruct_t&);
void func1();
void func2();

#endif

main.cpp

#include "driver.h"

int main(int, char **){
    myStruct_t myStruct;
    myStruct.a=1;
    myStruct.b=2;
    myStruct.c=3;

    driver(myStruct);
}

driver.cpp

#include "driver.h"

const int *a0,*b0,*c0;

void driver(const myStruct_t& myStruct){
    a0 = &myStruct.a;
    b0 = &myStruct.b;
    c0 = &myStruct.c;
    func1();
    func2();
}
void func1(){
    const int& a = *a0;
    const int& b = *b0;
    const int& c = *c0;
    // do stuff with a,b,c, such as:
    int d = a+b+c;
    ++d;
}
void func2(){
    const int& a = *a0;
    const int& b = *b0;
    const int& c = *c0;
    // do stuff with a,b,c, such as:
    int d = a+b+c;
    ++d;
}

除了显式存储和使用地址外,上面的内容与您的全局引用几乎相同。事实上,生成的机器代码可能是相同的。

顺便注意一下,我写的是 const int *a,*b,*c; 而不是 int *const a, *const b, *const c;。后者会定义 const 指针,这些指针通常很有用,但不是您想要的。相反,在这里,您需要指向 const.

的指针