如何使全局常量的别名在C ++的变量

问题描述 投票:1回答:2

我是相当新的C ++任何令人震惊的错误,非常抱歉。

问题陈述

我的代码是用于数学计算,所以main.cpp中/ main.h确实设置,在参数等读取,然后一个单独的文件/报头,把它叫做driver.cpp / driver.h,执行运算。

封装参数,我让几个用户定义的数据类型,在main.cpp中初始化它们,并将它们传递到driver.cpp定义的函数。我想对于被视为常数driver.cpp中的函数这些参数,而且我也想他们别名为可读性。这将是很好的每个功能Alias中他们,而不是一次。这可能吗?

我试图做一个简单的例子说明了我想要的东西,即使它不会跑,因为你不能使用常量引用我在下面做的方式。

我想要什么的想法:

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
}
c++ reference alias
2个回答
1
投票

如果指针为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。


2
投票

除非性能是一个因素,我建议不要依靠变量全局引用。我会建议使用功能接口访问它们。

// 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文件中,并在独立的文件,他们正在使用的文件的贯彻落实。

© www.soinside.com 2019 - 2024. All rights reserved.