Results 1 to 9 of 9

Thread: wrapper of methods

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default wrapper of methods

    Hello,
    I need to use pointer to members and retrieve the value (a double) that these members should return. But I have a compile error:
    Qt Code:
    1. #include <vector>
    2. class B {
    3. double _mean, _mean2;
    4. enum { cnt = 2 };
    5. public:
    6. double (B::*fptr[cnt])(void) const;
    7. B() {
    8. fptr[0] = &B::getMean;
    9. fptr[1] = &B::getMean2;
    10. }
    11. double getMean() const { return _mean; }
    12. double getMean2() const { return _mean2; }
    13. };
    14.  
    15. class A {
    16. public:
    17. std::vector<B> _vec;
    18.  
    19. };
    20. //main.cpp
    21. B objB;
    22. A objA;
    23. double db = objA._vec.at(0).fptr[0]; //here error; doens't B::mean() return a double?
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //error:
    2. error C2440: 'initializing' : cannot convert from 'double (__thiscall B::* )(void) const' to 'double'
    To copy to clipboard, switch view to plain text mode 
    Maybe i', using it in a wrong way.....how can I do this thing, please?
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrapper of methods

    You can't use pointer to member without an object, because it doesn't point to a method of particular object, but to a class member.

    Why do you need pointer to member here? Can't you overload an operator or use normal method?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: wrapper of methods

    Quote Originally Posted by jacek View Post
    You can't use pointer to member without an object, because it doesn't point to a method of particular object, but to a class member.
    Why do you need pointer to member here? Can't you overload an operator or use normal method?
    To be sincer, there I attached those simple methods for simplicity; but I need to attach 5 functions a bit more complex. what of these will be called depends from console parameters; then they are fixed at program start; what can be a situation where pointer to functions are needed?
    Isn't ................._vec.at(0). an instance of a class? I don't understand.....

    Can you give me an example of what you mean with overload an operator for this case?

    thanks,
    Regards

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: wrapper of methods

    try calling the function returned:
    Qt Code:
    1. double db = (objA.*(objA._vec.at(0).fptr[0]))();
    To copy to clipboard, switch view to plain text mode 

    (or some similar syntax ;-)

    HTH

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrapper of methods

    Quote Originally Posted by mickey View Post
    Isn't ................._vec.at(0). an instance of a class? I don't understand.....
    Yes, it it, but "objA._vec.at(0).fptr[0]" is an expression which returns a pointer to member. It doesn't mean "invoke the method that fptr[0] points to". As caduel wrote you have to use special syntax to invoke that method.

    Quote Originally Posted by mickey View Post
    Can you give me an example of what you mean with overload an operator for this case?
    The user of your class wants to perform some action depending on integer value. If you make him use pointers to members everywhere the code will be error-prone and unreadable. It is better to hide this inside your class and give your user only a convenient method and an enum:
    Qt Code:
    1. double db = obj.get( B::GeometricMean );
    2. // or using an operator:
    3. double db = obj[ B::GeometricMean ];
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: wrapper of methods

    hello,
    I understand the alternative but not why I shouldn't use pointertomembers.
    Anyway: What do you suggest for this case? The same?
    Qt Code:
    1. class A {
    2. public:
    3. trasformData1() { ......}
    4. trasformData2() {..........}
    5. trasformData3() { .....}
    6. //maybe here is better use another enum and one "TrasformData(enum);" ? I mean in the same way below....
    7. //trasFormData1,2,3 here are very different
    8. };
    9. class B {
    10. vector<A> _va;
    11. enum trasf { trasf1, trasf2, trasf3; }
    12. public:
    13. void trasform( trasf n) {
    14. switch (n) {
    15. case trasf1 :
    16. //loop on _va and call "_va.trasformData1();"
    17. break;
    18. case trasf2 : "call _va.trasformData2();" break;
    19. case trasf3 :"call _va.trasformData3();" break;
    20. default: exit(-1) //error
    21. };
    22. }
    23. };
    24. //main.cpp
    25. B b;
    26. //fill the vector
    27. //the type of trasformation is chose at command line
    28. b.trasform(B::Trasf1);
    To copy to clipboard, switch view to plain text mode 
    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrapper of methods

    Quote Originally Posted by mickey View Post
    why I shouldn't use pointertomembers.
    They're ugly, so if you want ot use them, hide them inside the class.

    Quote Originally Posted by mickey View Post
    Anyway: What do you suggest for this case? The same?
    How about this?

    Qt Code:
    1. TransformationFactory::TransformationFactory()
    2. {
    3. transformations[ SomeTransformation ] = new SomeTransformation();
    4. ...
    5. }
    6.  
    7. const Transformation * TransformationFactory::get( TransformationType type )
    8. {
    9. return transformations[ type ];
    10. }
    11.  
    12. ...
    13.  
    14. transformedVector = factory.get( SomeTransformation )->transform( originalVector );
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 14th August 2008 at 21:13. Reason: typo

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: wrapper of methods

    Quote Originally Posted by jacek View Post
    How about this?
    Qt Code:
    1. TransformationFactory::TransformationFactory()
    2. {
    3. transformations[ SomeTransformation ] = new SomeTransformation();
    4. ...
    5. }
    6. const Transformation * TransformationFactory::get( TransformationType type )
    7. {
    8. return transformations[ type ];
    9. }
    10. transformedVector = factory.get( SomeTransformation )->transform( originalVector );
    To copy to clipboard, switch view to plain text mode 
    Sorry, but I don't understand your example. My trasformation1,2,3 inside A are methods and not object (the do a normalization of the data).....maybe you understood they're objects? Isn't this a "pointer to member" way? I thought that you didn't suggest to use pointer to member..
    Last edited by mickey; 15th August 2008 at 10:50.
    Regards

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrapper of methods

    Quote Originally Posted by mickey View Post
    My trasformation1,2,3 inside A are methods and not object (the do a normalization of the data).....
    Yes and my proposition is to change them into objects.

    Quote Originally Posted by mickey View Post
    Isn't this a "pointer to member" way? I thought that you didn't suggest to use pointer to member..
    It's very similar, but you can avoid that ugly syntax. Pointers to members will be OK too if you hide them behind some nice interface.

Similar Threads

  1. How to access OCX methods and events
    By joy in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2018, 17:37
  2. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 17:08
  3. Signal/Slot methods
    By Micawber in forum Newbie
    Replies: 4
    Last Post: 3rd August 2007, 16:23
  4. can't reuse a pure c++ .h file's methods and classes
    By sincnarf in forum General Programming
    Replies: 11
    Last Post: 3rd August 2007, 04:39
  5. The Methods of quick search...
    By Xagen in forum General Discussion
    Replies: 1
    Last Post: 27th February 2006, 20:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.