Question:
C++: Callback member functions?
1970-01-01 00:00:00 UTC
C++: Callback member functions?
Three answers:
intriago
2016-10-20 01:30:54 UTC
C has one group of policies and C++ has 3 communities. In C you might have a pointer to any kind, inclusive of information that ought to policies. So given any kind you might have a pointer kind to it. typedef T* PointerTypeToT; In C++ you have the comparable undemanding pointer varieties you have in C, plus you have a definite pointer kind spoke of as a reference. typedef T& ReferenceTypeToT; A reference kind is a pointer that makes use of fee kind syntax to get right of entry to it and must be initialised on statement. that's greater tricky (yet no longer impossible) to assign NULL values to a reference kind. finally C++ has member policies: typedef T S::* PointerToTypeTInS; that's a pointer that factors right into a given type. If a type has quite a few member int individuals a, b and c, then this facilitates you to create a pointer that factors to a form of individuals for any given occasion of the class. the two C and C++ evaluate purposes to be basically particular forms of goods, so each and every of the above applies to function policies.
oops
2011-03-18 09:31:23 UTC
For the constructor problem here's what I would do. Instead of making a bunch of different constructors, I would just make free functions that create objects and return them. Here's a simplified example:



class X

{

public:

    X() { }

    X(int ia, int ib, int ic) :a(ia), b(ib), c(ic) { }

private:

    int a,b,c;

};



X CreateUninitializedX() { return X(); }

X CreateZeroInitializedX() { return X(0,0,0); }

X CreateRandomizedX() { return X(rand(),rand(),rand()); }



For your other problem, yes, C++ does have member function pointers. But I find their syntax quite odd, and I have never had occasion to use them, so I cannot advise you on them. However, I am quite positive that they are unnecessary in most situations, and can instead be replaced by free functions, just like the constructor issue. I'd need more details about your specific problem to be able to help you though.
cja
2011-03-19 12:11:31 UTC
Good question. It's nice to see someone thinking about the design of their program instead of just jumping in and hacking out some code. There are many ways to do this. One way would be for your Array class to have an association with an array initializer. An initializer is given on construction, and different initializers can be plugged in as needed. Here's a simple example:



#include

#include

#include

#include



using namespace std;



template class Array;

template

class ArrayInitializer {

public:

    ArrayInitializer() { }

    virtual void init(Array&) = 0;

};



template

class Array {

public:

    Array(size_t n, ArrayInitializer *ia) : _len(n), _ia(ia) {

        _a = new T[_len];

    }

    void setItsArrayInitializer(ArrayInitializer *ia) {

        _ia = ia;

    }

    void init() { _ia->init(*this); }

    inline T* getAptr() const { return _a; }

    inline size_t len() const { return _len; }

    ~Array() {

        delete [] _a;

    }

private:

    Array() { }

    const size_t _len;

    ArrayInitializer *_ia;

    T *_a;

};



template

class ZeroizeInitializer : public ArrayInitializer {

public:

    ZeroizeInitializer() { }

    void init(Array &arr) {

        T *arrPtr = arr.getAptr();

        fill(arrPtr, arrPtr + arr.len(), 0);

    }

};



template

class RandomIntInitializer : public ArrayInitializer {

public:

    RandomIntInitializer(T maxRand) : _maxRand(maxRand) { }

    void init(Array &a) {

        srand((unsigned)time(0));



        T *arr = a.getAptr();

        for (size_t i = 0; i < a.len(); i++) {

            arr[i] = static_cast(rand()) % _maxRand;

        }

    }

private:

    RandomIntInitializer() { }

    T _maxRand;

};



template

class RandomFltInitializer : public ArrayInitializer {

public:

    RandomFltInitializer() { }

    void init(Array &a) {

        srand((unsigned)time(0));



        T *arr = a.getAptr();

        for (size_t i = 0; i < a.len(); i++) {

            arr[i] = rand();

        }

    }

};



template

ostream &operator<<(ostream &os, const Array &a) {

    T *arr = a.getAptr();

    for (size_t i = 0; i < a.len(); i++) {

        os << arr[i] << ' ';

    }

    return os;

}



int main(int argc, char *argv[]) {

    RandomIntInitializer *randomInitI = new RandomIntInitializer(100);

    RandomFltInitializer *randomInitF = new RandomFltInitializer();

    ZeroizeInitializer *zeroInit = new ZeroizeInitializer();

    Array intArray(10, randomInitI);

    Array fltArray(5, randomInitF);



    intArray.init();

    cout << intArray << endl;

    fltArray.init();

    cout << fltArray << endl;

    intArray.setItsArrayInitializer( zeroInit );

    intArray.init();

    cout << intArray << endl;



    delete randomInitI;

    delete randomInitF;

    delete zeroInit;

    return 0;

}



#if 0



Sample run:



26 99 3 98 68 90 44 58 30 71

1.60633e+09 4.19122e+08 1.87931e+08 6.99658e+08 1.94081e+09

0 0 0 0 0 0 0 0 0 0



#endif


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...