Posiadam taki kod
main.cpp
#include <iostream>
#include "complex.h"
#include <math.h>
using namespace std;
int main()
{
Complex c1(3,5);
Complex c2(2,7);
cout<<c1.tab_wsk_fun[3]<<endl;
cout<<c1.fun3()<<endl;
cout<<c1.fun4()<<endl;
return 0;
}
complex.cpp
#include <iostream>
#include "complex.h"
#include <math.h>
using namespace std;
Complex::Complex()
{
re=0;
im=0;
tab_wsk_fun[0]=&Complex::fun1;
tab_wsk_fun[1]=&Complex::fun2;
tab_wsk_fun[2]=&Complex::fun3;
tab_wsk_fun[3]=&Complex::fun4;
}
Complex::Complex(double rze)
{
re=rze;
im=0;
tab_wsk_fun[0]=&Complex::fun1;
tab_wsk_fun[1]=&Complex::fun2;
tab_wsk_fun[2]=&Complex::fun3;
tab_wsk_fun[3]=&Complex::fun4;
}
Complex::Complex(double rze, double ur)
{
re=rze;
im=ur;
tab_wsk_fun[0]=&Complex::fun1;
tab_wsk_fun[1]=&Complex::fun2;
tab_wsk_fun[2]=&Complex::fun3;
tab_wsk_fun[3]=&Complex::fun4;
}
double Complex::fun1()
{
return re;
}
double Complex::fun2()
{
return im;
}
double Complex::fun3()
{
return (re*re+im*im);
}
double Complex::fun4()
{
return (sqrt(re*re+im*im));
}
complex.h
#include <iostream>
using namespace std;
class Complex
{
double re, im;
public:
double ( Complex::*tab_wsk_fun[4])();
Complex();
Complex(double);
Complex(double, double);
double fun1();
double fun2();
double fun3();
double fun4();
};
Chciałbym w main wyświetlic wartość na jaką wskazuje wskaźnik np. tab_wsk_fun[3] ale cały czas mi wypisuje 1 i nie wiem gdzie mam błąd. W jaki sposób moge wypisac to co siedzi pod wskaźnikiem tab_wsk_fun? Muszę przeładować << czy w jakiś inny sposób?