程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c++中比static結構體更好用的namespace

c++中比static結構體更好用的namespace

編輯:關於C語言
 

經驗證 struct 中包含static情況下,可以用

 

namespace tao{

int hai;

}

來替換 。應用時只需要:tao::hai.

 

 

 

#ifndef _MY_HPP_
#define _MY_HPP_

namespace myns{
int x = 100;
}

void fun();

#endif

 


 


 

I want to define an structure, where some math constants would be stored.
Here what I've got now:

struct consts {
    //salt density kg/m3
   static const double gamma;
};

const double consts::gamma = 2350;

It works fine, but there would be more than 10 floating point constants, so I doesn't want to wrote 'static const' before each of them. And define something like that:

static const struct consts {
    //salt density kg/m3
   double gamma;
};

const double consts::gamma = 2350;

It look fine, but I got these errors:
1. member function redeclaration not allowed
2. a nonstatic data member may not be defined outside its class

I wondering if there any C++ way to do it?

 

Use a namespace rather than trying to make a struct into a namespace.

namespace consts{
    const double gamma = 2350;
}

The method of accessing the data also has exactly the same synatx. So for example:

double delta = 3 * consts::gamma;
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved