A
A
Aidsoid2010-11-15 13:59:02
Qt
Aidsoid, 2010-11-15 13:59:02

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 ;
} ;


And there is a Pair class, which should store a list / array of pointers to instances of the Test class and some other parameters (properties)

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 ;
} ;


It is supposed to create many descendants of the Pair class and within these descendants to create instances of the Test class. But in order to pass the address of the Test1() function, it must be inside the static class, but at the same time I need to have access to the properties of this class inside this function, for example, this->property("Name"), but inside the static function this is not available. What to do, how to be? The third day I struggle with it.

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 ;
    }
} ;


And it's supposed to be used like this:

Pair01  * pr01  =  new  Pair01 ( ) ;  // create an instance of class Pair01
pr01 - > Tests [ 0 ] - > Start ( ) ;  // run the first test


I hope at least some of this is clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
burdakovd, 2010-11-15
@Aidsoid

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 question

Ask a Question

731 491 924 answers to any question