осуществление посещения класса в другом классе

Я пытаюсь определить тип Boost :: варианта в объекте класса для выполнения связанных функций-членов. Рассмотрим следующий код:

#include <cstdio>
#include <cassert>
#include <iostream>
#include <boost/variant.hpp>
#include <string>
using namespace std;

typedef boost::variant< string, double> Variant;

class test{
public:
void func1 (Variant V);
void func2 (string s);
void func3 (double d);
struct my_visitor : public boost::static_visitor<> {
test &my_test;
my_visitor(test &arg) : my_test(arg) {}

void operator()( string s ) { my_test.func2(s); }
void operator()( double d ) { my_test.func3(d); }
//... for each supported type
};

};void test::func1(Variant V){
boost::apply_visitor( my_visitor(*this),V);
}

void test::func2( string s){ cout << "func3" << endl;}
void test::func3(double d){ cout << "func4" << endl;}

int main (){
test t;
Variant V = 3.1;
t.func1(V);
V = "hello";
t.func1(V);
}

Проблема в том, что мне нужно определить тип Variant в функции-члене, чтобы вызвать связанную функцию-член для этого типа данных EVEN в том же объекте.

0

Решение

Вопрос не совсем понятен, но вы, возможно, ищете это?

class test {
//...

struct my_visitor : public boost::static_visitor<> {
test &my_test;
my_visitor(test &arg) : my_test(arg) {}

void operator()( string s ) const { my_test.func3(s); }
void operator()( double d ) const { my_test.func4(d); }
//... for each supported type
};

//...
};test::func1(Variant V) {
boost::apply_visitor(my_visitor(*this), V);
}

РЕДАКТИРОВАТЬ добавленной const квалификаторы для operator() разрешить использование временных my_visitor в apply_visitor(),

3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector