Answer the question
In order to leave comments, you need to log in
C++ OOP and function overriding
There is a Test class, which should store various parameters of a certain test and a function for executing this certain test (the Start () function). Moreover, the Start function should not be stored directly in this class, it should be redefined, that is, for each instance of the test class, it should be its own.
At the moment it looks like this:
class Test : public QObject
{
Q_OBJECT
Q_PROPERTY ( QString Name READ getName ) ;
public :
// constructor
Test ( QString Name, int ( * func ) ( ) )
{
this - > name = Name ;
this - > functionStart = func ;
}
// property: getting name
QString getName ( ) const
{
return name ;
}
// start test
int Start ( )
{
return ( * functionStart ) ( ) ;
}
private :
int ( * functionStart ) ( ) ;
QString name ;
} ;
class Pair : public QObject
{
Q_OBJECT
Q_PROPERTY ( QString Id READ getId ) ;
public :
// constructor
Pair ( QString Id )
{
this - > id = Id ;
}
// property: getting id
QString getId ( ) const
{
return id ;
}
// array of tests
QList < Test * > Tests ;
private :
QString id ;
} ;
class Pair01 : public Pair
{
public :
Pair01 ( ) : Pair ( "01" )
{
Tests. append ( new Test ( tr ( "Test 1" ) , Test1 ) ) ;
tests. append ( new Test ( tr ( "Test 2" ) , Test2 ) ) ;
tests. append ( new Test (tr ( "Test 3" ) , Test3 ) ) ;
}
static int Test1 ( )
{
// here's the hell! You need to access this-> inside this function
}
static int Test2 ( )
{
return 0 ;
}
static int Test3 ( )
{
return 0 ;
}
} ;
Pair01 * pr01 = new Pair01 ( ) ; // create an instance of class Pair01
pr01 - > Tests [ 0 ] - > Start ( ) ; // run the first test
Answer the question
In order to leave comments, you need to log in
As far as I understand you need something like a closure.
Although I'm not sure, the abundance of unfamiliar Qt scares me.
If you pass functions in the C style, then it’s a fail, you can pass a pointer to a function or a static method, and from there you can’t get access to this, that’s why it’s static.
In C style, this can be solved by making the function signature not int (*func)(), but int (*func)(void*). That is, the function expects an execution context as a parameter. And in the Test constructor, in addition to the function, pass this context. Well, in this void* you can pass this.
In C++ style, you could do this:
class Runnable() {
virtual int run() = 0;
}
And the Test constructor will take not a function pointer, but a Runnable* type pointer.
Well, then it’s a matter of technology, inside Pair01 you can create an auxiliary class that implements the Runnable interface, which will store a pointer to the Pair01 instance and call the already non-static Test1 or Test2 or Test3 in the Run method ...
In languages with closure support, this would be trivial, but in C ++ you have to implement them explicitly.
Also, boost probably has functions for this, I didn’t use it myself, but I saw somewhere that this was solved using bind1st.
In turn, I hope that something from this is clear =)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question