Thread: how to convert return type of function at run time ?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For Example,in Micrsoft Paint brush user will select the circle object and draw it means he can use any object at any time .
    The commonality in what you described is that the user will select a shape. A circle is a shape. A rectangle is a shape. One can enlarge a circle. One can move a circle. One can enlarge a rectangle. One can move a rectangle.

    There is something in common there, and we can say that one can move a shape, and one can enlarge a shape. So with a shape base class, the choice of functions is limited to those public member functions specified in shape, even though there may be many more derived class member functions that override these few shape virtual member functions.

    If I code like this then no need to write switch case. The ((Generic*)obj_ptr[class_id]->*pt[class_id][Fn_id])(); line will do all things for me ! right
    It will not. You do not understand: according to your code, a Generic is a store, a Generic is a purc, and a Generic is a manu. Typecasting is useless.

    You could do the opposite: create a Generic base class (similiar to the Object class in Java and many other OO languages). But then you still have to use a switch of some sort to typecast.

    In other words, if there is no commonality, then you have no choice but to use a massive switch. If there is commonality, you can factor it out into a class hierarchy and use polymorphism.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the circle, square and painbrush can all be implemented as an object with a baseclass of:
    Code:
    class basicdrawobject
    {
    public:
       void drawat(int x, int y);
       void setColor(int rgb);
       ...
    };
    But you still need to CREATE a circle object if you want to draw a circle.

    There are ways that you can have a common base object that does the necesary operations based on a small set of public functions, where the internal object has completely different capabilities.

    But using the "generic object" like you do is absolutely not the right option.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    If I have a Function pointer array of void type and storing few function with different return type. I dont know which function user is going to call at run time, so I need to code in such a way that the compiler should typecast the function return type from void function pointer array.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't cast function pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    Not function pointer. When I need to call a function from that array first I need to type cast that function to its original return type right.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You can't cast function pointers.
    Sure you can - as shown in the second or third post in this thread - however, it's not a particularly clever idea to do that, and the code is definitely not pretty afterwards.

    Vinod: So you are going to end up with a 500 case switch statement just to sort out your 500 different casts - and the code will be completely unreadable. YOU ARE NOT USING THE RIGHT SOLUTION!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Sure you can - as shown in the second or third post in this thread - however, it's not a particularly clever idea to do that, and the code is definitely not pretty afterwards.
    Hmmm... maybe I remember wrong. There is something about them I remember can't be cast.
    I'll admit I haven't read the code in the thread, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    So tell me how can I typecast the function return type from pointer function array at run time ?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vinod_mrd View Post
    So tell me how can I typecast the function return type from pointer function array at run time ?
    I did, about post #3 or so in this thread. It doesn't look particularly pretty, and you still need to have your switch statement to make sure that the right function is called with the right return type, so you're not really saving anything - just making the code unreadabale and unmaintainable (and very fragile, as the compiler won't tell you when you get it wrong).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    I'll solve this and will be back with my solutions friends. Because Nothing is Impossible in this world.............

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vinod_mrd View Post
    I'll solve this and will be back with my solutions friends. Because Nothing is Impossible in this world.............
    No, it's not impossible - but just because something CAN be done, doesn't mean it's the right thing to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'll solve this and will be back with my solutions friends.
    Good luck.

    Because Nothing is Impossible in this world.
    Getting your code featured on The Daily WTF (a.k.a. Worse Than Failure) would be very possible indeed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM