Я не совсем понимаю, почему вы хотите предоставить реализацию по умолчанию для двух других функций, но требуется, чтобы хотя бы одна из них была определена пользователем в случае http-запросов.
Понятно, что все функции используют друг друга для реализации некоторых функций с использованием существующего кода.
Представьте себе пример с этим классом:
class Cls
{
public:
virtual std::string toString()=0;
virtual std::string serialize()=0;
};
Существует класс, который можно преобразовать в строку и сериализовать в строку. Но если один из них не реализован, вам нужно вместо этого вызвать второй, так что это будет вариант:
class Cls
{
public:
virtual std::string toString() //calls serialize() by default
{
return this->serialize();
}
virtual std::string serialize() //calls toString()
{
return this->toString();
}
virtual ~Cls()=0; //force the class to be abstract
}; Cls::~Cls(){}
Но теперь существует проблема с производным от Cls, но не переопределяющим хотя бы одну из функций. Если переопределение не выполняется, во время выполнения вы просто вводите бесконечную рекурсию. Если это одна из ваших проблем, есть решение во время выполнения, приведенный ниже код просто ничего не делает, если возникает такая проблема.
class Cls
{
public:
virtual std::string toString()
{
if ((void*)(this->*(&Cls::serialize)) != (void*)(&Cls::serialize))
{//checks if the current implemetation is not equal to the default one
return this->serialize();
}
else
{
return ""; //default return value
}
}
virtual std::string serialize()
{
if ((void*)(this->*(&Cls::toString))!=(void*)((&Cls::toString)))
{
return this->toString();
}
else
{
return "";
}
}
virtual ~Cls()=0;
}; Cls::~Cls(){}
Это компилируется в GCC, но заполняет ваш экран предупреждениями о странном преобразовании из funcptr в void *. По крайней мере, это работает как задумано. Могут быть некоторые метапрограммирующие решения времени компиляции, нужно подумать об этом.
Приложение 1 , тестирование сравнения между функциями-членами:
Это действительно странно
#include <iostream>
class Base
{
public:
virtual int test()
{
//default imp
return 0;
}
};
class Der : public Base
{
public:
int test() override
{
//custom imp
return 1;
}
};
int main()
{
Der a;
Base b;
std::cout << ((&Der::test) == (&Base::test)) << std::endl;//1: wrong
//they are not equal
//but for some reason the output is "true"
//so direct comparisons do not work
//however
//if you convert that pointer to void*
//everything works
std::cout << ((void*)(&Der::test) == (void*)(&Base::test) ) << std::endl; //0:right
std::cout << ((void*)(a.*(&Base::test)) == (void*)(&Base::test) ) << std::endl;//0:right
std::cout << ((void*)(b.*(&Base::test)) == (void*)(&Base::test) ) << std::endl;//1:right
std::cout << ((void*)(&(a.test)) == (void*)(&(b.test)) ) << std::endl; //0:right
//so if you want to compare two functions
//cast them to void*
//works in any cases
//'-Wno-pmf-conversions' compiler flag to inhibit warnings about casting
system("pause");
return 0;
}
Приложение2 , этапы получения реального адреса функции:
Cls::serialize; //the function object itself
&Cls::serialize; //its member pointer
(void*)(&Cls::serialize); //extracting real address of the function for the comparison
(this->*&Cls::serialize); //again, a member pointer
(void*)(this->*&Cls::serialize); //extracting real address
// │ │ └── Getting "pointer" to a member function of the class
// │ └───── Then binding 'this' to that function, recall that if the function is virtual, '->*' returns a mamber pointer to it's custom implementation, not the default one.
// └────────────── Then getting the real address
// it looks like 'this->*&Cls::serialize' does the same as '&this->serialize'
// but in practice it's not quite right
// '&this->serialize' returns the function pointer based on 'this' type
// therefore, comparison breaks, as inside of a base class 'this' always has the base type
// so you always receive the default implementation pointer
// 'this->*&Cls::serialize' does the same
// but now if 'serialize' is virtual
// it takes it into account and sends back its' custom implementation pointer
// (void*) casting is required because you need to compare functions' real addresses
// if you compare member pointers of a single virtual function
// they seem to be equal while they are, in fact, not
Проблема проверки, реализовал ли производный класс некоторые виртуальные функции базового класса, здесь