아마도이 질문과 함께 왼쪽 필드 밖에 있습니다.하지만 생성자를 통해 멤버 함수를 정의 할 수 있습니까?생성자를 통해 멤버 함수를 초기화하십시오.
필자의 경우 강력한 모델 피팅 (RANSAC 사용)을 수행 할 클래스를 작성하려고합니다. 나는 이것이 다른 유형의 모델에 일반화되기를 바란다. 예를 들어, 이것을 사용하여 3D 점 집합에 대한 평면의 추정을 결정할 수 있습니다. 또는 아마도 두 세트의 점 사이의 변형을 결정할 수 있습니다. 이 두 예제에서 서로 다른 오류 함수와 다른 피팅 함수가 필요할 수 있습니다. 대신 클래스를 사용하는 정적 함수 호출 내가 그 모듈 기능 멤버 인스턴스를 가질 수 있는지 궁금하네요
model = estimate(data, &fittingFunc, &errorFunc);
처럼 보일 수 있습니다?
class Estimator
{
private:
// estimation params
double errorFunc(std::vector<dtype>, double threshold); // Leave this unimplemented
double fittingFunc(std::vector<dtype>, Parameters p); // Leave this unimplemented
public:
Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double));
dtype estimate(data); // Estimates model of type dtype. Gets implemented
};
Estimator::Estimator(void (*fittingFunc(std::vector<dtype>, Parameters), void (*errorFunc(std::vector<dtype>, double))
{
fittingFunc = fittingFunc;
errorFunc = errorFunc;
}
같은
는뭔가 내가 내 예에 적절한 구문을 놨 한 상상,하지만 문제는 분명하다 바랍니다. 기본적으로 묻는 오전 : 생성자 인수로 함수 포인터를 받아들이고 멤버 함수의 구현을 할당 할 수 있습니까?
두 번째로 이것이 가능하더라도 나쁜 형태로 간주됩니까?
UPDATE : 그것은, 내가 C++
이
질문에 대한 대답은 ** 예 **입니다 :
클래스는 다음과 같이 될 것이다. 'double에서 코드를 수정하십시오. '; 그리고이 세미콜론은 무엇을위한 것입니까? –
@ k-five : 오타, 미안. 그리고 네, 가능합니까? 아니면 그렇습니다. 나쁜 형태입니까? 아니면 둘다? – marcman
[*** Pimpl idiom ***] (http://stackoverflow.com/questions/60570/why-should-the-pimpl-idiom-be-used)이 더 나은 접근 방법 일 수 있습니다. –