Static array containing non static objects and functions 
Author Message
 Static array containing non static objects and functions

// I am using VC 5.0 on NT platform.
// I have a code that is very similar to the one below that I have to maintain.
// I have problem with it at run-time. The static array ms_aUtils does not get
// initialised properly - that is m_UtilPtr is always NULL.
// I also have some difficulties to understand the code, especially the place:
//              PowerTo UtilInitialiser::* m_UtilPtr;
// I have added Init function to it but is still does not help.
// If I replace &UtilInitialiser::m_Power with (PowerTo UtilInitialiser::*)(&m_Power) I
// get this error:(the same happens when I use c++ cast operators)
// 'type cast' : cannot convert from
// 'class PowerTo *' to 'class PowerTo UtilInitialiser::*'
//  There is no context in which this conversion is possible
// What does that mean - is a pointer to PowerTo that is contained by UtilInitialiser ?.
// I would appreciate any help.
// Janusz

#include "windows.h"

#include <list>
#include <queue>
#include <string>
#include <ios>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <limits>
#include <xutility>
#include <xmemory>
#include <map>
#include <set>
#include <numeric>

class PowerTo
{
public:
        PowerTo(int power_to){m_PowerTo = power_to;};
        operator()(int val)
        {
                double res = pow(val, m_PowerTo);
#ifdef _DEBUG
                std::cout << val << "Powered to" << m_PowerTo << "Equal::" << res;
#endif
                return res;
        }
private:
        int     m_PowerTo;

Quote:
};

class UtilInitialiser
{
public:
        UtilInitialiser():m_Power(10){};
        void Init()
        {
                ms_aUtils[0].m_UtilPtr = &UtilInitialiser::m_Power;
                ms_aUtils[0].m_fn = UtilInitialiser::DispResult;
        }
        void Test() ;
        void DispResult(double res)
        {
                std::ostringstream ostr;
                ostr << "Result of the operation::" << res;
                std::cout << ostr.str();
        }

protected:
        PowerTo m_Power;
private:
        class UtilInfo
        {
                public:
                PowerTo UtilInitialiser::* m_UtilPtr;
            void (UtilInitialiser::* m_fn)(double); // Processing function
                double                  m_Result;
        };
  static UtilInfo ms_aUtils[];

Quote:
};

void UtilInitialiser::Test()
{
        for (UINT i = 0; NULL != ms_aUtils[i].m_UtilPtr; i++)
        {
                this->ms_aUtils[i].m_Result = (this->*ms_aUtils[i].m_UtilPtr)(2);
                (this->*ms_aUtils[i].m_fn)(this->ms_aUtils[i].m_Result);
        }

Quote:
}

UtilInitialiser a;

UtilInitialiser::UtilInfo UtilInitialiser::ms_aUtils[] =
{
        {&UtilInitialiser::m_Power, UtilInitialiser::DispResult, 0},
        {NULL, NULL, 0}

Quote:
};

void main()
{
        a.Init();

        std::ostringstream ostr;
        a.Test();

        getchar();

Quote:
}

-----------------** -- Posted from CodeGuru -- **-----------------
http://www.*-*-*.com/ ;  The website for Visual C++ programmers.


Fri, 16 Nov 2001 03:00:00 GMT  
 Static array containing non static objects and functions
What you've got there is a "pointer to member".  A pointer-to-member points to a particular
member function or data member in an object yet to be specified.  You then invoke the
pointer-to-member through an instance of the class.

E.g.,
    class foo
    {
    public:
        foo( int i1, int i2 ) : m_i1(i1), m_i2(i2) {}
        int m_i1, m_i2 ;
    };

    // point to member var m_i2 (actual object not yet specified)
    int foo::* pFooI = &foo::m_i2 ;

    void main()
    {
        // create an instance of foo
        foo f( 1, 2 ) ;
        // use .* notation to invoke pointer-to-member with this object
        std::cout << f.*pFooI << std::endl ;

        // Another example, this time through a pointer
        foo * pF = &f ;
        // Use ->* notation to invoke through a pointer
        std::cout << pF->*pFooI << std::endl ;

    }

HTH

-- Aaron [MVP]

Quote:

> // I am using VC 5.0 on NT platform.
> // I have a code that is very similar to the one below that I have to maintain.
> // I have problem with it at run-time. The static array ms_aUtils does not get
> // initialised properly - that is m_UtilPtr is always NULL.
> // I also have some difficulties to understand the code, especially the place:
> //              PowerTo UtilInitialiser::* m_UtilPtr;
> // I have added Init function to it but is still does not help.
> // If I replace &UtilInitialiser::m_Power with (PowerTo UtilInitialiser::*)(&m_Power) I
> // get this error:(the same happens when I use c++ cast operators)
> // 'type cast' : cannot convert from
> // 'class PowerTo *' to 'class PowerTo UtilInitialiser::*'
> //  There is no context in which this conversion is possible
> // What does that mean - is a pointer to PowerTo that is contained by UtilInitialiser ?.
> // I would appreciate any help.
> // Janusz

> #include "windows.h"

> #include <list>
> #include <queue>
> #include <string>
> #include <ios>
> #include <iomanip>
> #include <fstream>
> #include <iostream>
> #include <fstream>
> #include <sstream>
> #include <memory>
> #include <limits>
> #include <xutility>
> #include <xmemory>
> #include <map>
> #include <set>
> #include <numeric>

> class PowerTo
> {
> public:
>         PowerTo(int power_to){m_PowerTo = power_to;};
>         operator()(int val)
>         {
>                 double res = pow(val, m_PowerTo);
> #ifdef _DEBUG
>                 std::cout << val << "Powered to" << m_PowerTo << "Equal::" << res;
> #endif
>                 return res;
>         }
> private:
>         int     m_PowerTo;
> };

> class UtilInitialiser
> {
> public:
>         UtilInitialiser():m_Power(10){};
>         void Init()
>         {
>                 ms_aUtils[0].m_UtilPtr = &UtilInitialiser::m_Power;
>                 ms_aUtils[0].m_fn = UtilInitialiser::DispResult;
>         }
>         void Test() ;
>         void DispResult(double res)
>         {
>                 std::ostringstream ostr;
>                 ostr << "Result of the operation::" << res;
>                 std::cout << ostr.str();
>         }

> protected:
>         PowerTo m_Power;
> private:
>         class UtilInfo
>         {
>                 public:
>                 PowerTo UtilInitialiser::* m_UtilPtr;
>             void (UtilInitialiser::* m_fn)(double); // Processing function
>                 double                  m_Result;
>         };
>   static UtilInfo ms_aUtils[];
> };

> void UtilInitialiser::Test()
> {
>         for (UINT i = 0; NULL != ms_aUtils[i].m_UtilPtr; i++)
>         {
>                 this->ms_aUtils[i].m_Result = (this->*ms_aUtils[i].m_UtilPtr)(2);
>                 (this->*ms_aUtils[i].m_fn)(this->ms_aUtils[i].m_Result);
>         }
> }

> UtilInitialiser a;

> UtilInitialiser::UtilInfo UtilInitialiser::ms_aUtils[] =
> {
>         {&UtilInitialiser::m_Power, UtilInitialiser::DispResult, 0},
>         {NULL, NULL, 0}
> };

> void main()
> {
>         a.Init();

>         std::ostringstream ostr;
>         a.Test();

>         getchar();
> }

> -----------------** -- Posted from CodeGuru -- **-----------------
> http://www.codeguru.com/    The website for Visual C++ programmers.



Fri, 23 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. One Problem in Calling Static function from Non static function

2. Accessing non-static functions from a static function

3. Pointers to non-static vs static functions

4. static functions and non static variables

5. static functions with non-static variables!

6. Static and non-Static member - Signature

7. Creating a static class from non static Framework classes

8. static versus non static returnvalues

9. Linking Error - Static vs. Non-Static

10. Linking Static DLL vs Non Static DLL

11. Getting pointer to non-static member function from C callback function

12. Using Non-Static Callback Functions as member Functions VC5.0

 

 
Powered by phpBB® Forum Software