C++のconstメンバ関数、非型テンプレートや外部シンボル
#pragma once
class functions {
public:
static const int addition(const int&, const int&);
static const int product(const int&, const int&);
};
functions.cpp
私も、私が選択する機能のどの定義するプログラム設定のためのセクションを作成しました#include "functions.h"
const int functions::addition(const int& op1, const int& op2) {
return op1 + op2;
}
const int functions::product(const int& op1, const int& op2) {
return op1 * op2;
}
:
次のサンプルを使用して期待通りconstants.h
#pragma once
const int modulus = 10;
extern const int(*operation)(const int&, const int&);
constants.cpp
#include "constants.h"
#include "functions.h"
const int(*operation)(const int&, const int&) = &functions::addition;
は、このコードは動作します:
main.cppに
#include <iostream>
#include "constants.h"
int main() {
int a = 7, b = 4;
std::cout << operation(a,b) % modulus << std::endl;
}
問題は、あります今私はfuをparametrizeしたいと思います以下のような非型テンプレートを使用してnctionsのほか、製品、:
functions.h
#pragma once
class functions {
public:
template<const int&> static const int addition(const int&, const int&);
};
どんなに私が適応しようとする方法をfunctions.cpp
#include "functions.h"
template<const int& sub> const int functions::addition(const int& op1, const int& op2) {
return op1 + op2 - sub;
}
後でコードブレーク、他のファイル。私は合理的に(そして無意識のうちに)想像できるすべてをやった。
定数*。でコードを書き直す方法についてのヘルプは非常に高く評価されます。
サイドノート:あなたはファーストクラスは必要ありません。あなたの関数に別の名前空間が必要な場合は、名前空間を使います。 – molbdnilo
あなたのアドバイスをありがとうございますが、クラスは、指定された機能を扱う他の機能をグループ化しています。彼らはこの問題で何の役割も果たしていないので、私はそれらを最小限の例から除外することに決めました。 – jvier
私はそれがどう重要であるかわかりません。静的メンバーだけを持つクラスはJava/C#/ Smalltalkのものです。 – molbdnilo