簡體   English   中英

如何在C ++中為變量創建全局常量別名

[英]How to make global constant aliases to variables in C++

我對C ++很陌生,對任何令人震驚的錯誤都很抱歉。

問題陳述

我的代碼用於數學計算,因此main.cpp / main.h進行設置,讀入參數等,然后單獨的文件/標題,稱之為driver.cpp / driver.h,進行計算。

為了封裝參數,我創建了幾個用戶定義的數據類型,在main.cpp中初始化它們,並將它們傳遞給driver.cpp中定義的函數。 我想將這些參數視為driver.cpp中函數的常量,並且我還想為它們提供別名以便於閱讀。 將它們別名化為一次而不是在每個函數中都是很好的。 這可能嗎?

我試圖做一個簡單的例子來說明我想要的東西 ,即使它不會運行,因為你不能像我下面那樣使用常量引用。

我想要的想法:

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的指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM