Thread: can i do instance polymorphism?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    can i do instance polymorphism?

    is possible do a instance polymorphism?
    i understand that i can create class's with virtual functions, but i only can change them with another class and not with objects\instances

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I fail to understand what you mean. Example?
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am not sure if you mean prototype based inheritance or monkey patching, but as far as I know, neither is (normally) possible in standard C++.
    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

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    sorry to both... think in these way:
    Code:
    #include <iostream>
    using namespace std;
    class test
    {
       public:
              virtual void print(){};
    
    };
    
    class test1 : test
    {
       public:
              void print();
    };
    
    void test1::print()
    {
          cout << "hello world";
    
    }
    these is the class polymorphism.(code not tested)
    (sorry, if i'm mistake in something, please tell... i'm 'new' with some concepts)

    now see these 2 example:
    Code:
    #include <iostream>
    using namespace std;
    
    class test
    {
       public:
              virtual void print(){};
    
    };
    
    test a, b;
    
    void a::print()
    {
         cout << "hello world\n";
    }
    
    void b::print()
    {
         cout << "hello C Board Programing\n";
    }
    
    
    int main()
    {
         a.print();
         b.print();
        cin.get();
    }
    i don't know if you understand what i mean... but i'm trying
    (i'm portuguese)
    Last edited by joaquim; 02-01-2014 at 12:35 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can't be done.
    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

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    Can't be done.
    even with adicional class's or precompiler code?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    even with adicional class's or precompiler code?
    The effect that you have in mind can be done in standard C++, e.g., by assigning a unique id to each object and then selecting the behaviour based on this id. Why do you want to do this? Perhaps a better way can be suggested.
    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

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    The effect that you have in mind can be done in standard C++, e.g., by assigning a unique id to each object and then selecting the behaviour based on this id. Why do you want to do this? Perhaps a better way can be suggested.
    is for build events and change the events outside of the main function. i have the:
    Code:
    typedef std::function<void(void)> OnSomethingHandler;
    but i can't change the values outside of the main

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    sorry, i mean:
    Code:
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    is for build events and change the events outside of the main function
    I don't know what you mean by "build events" and I don't know exactly what you are trying to change and where. Logically though, if you have a callback function (object), you can replace it at run time.
    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

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    I don't know what you mean by "build events" and I don't know exactly what you are trying to change and where. Logically though, if you have a callback function (object), you can replace it at run time.
    sorry my english
    but what i realy need is these:

    returntype objectname::functionname()//with arguments or not

    but doing outside of the main function...
    sorry but i'm problems to express better but i think the MFC way, can give you a clue... i think. or the Visual C++ form applications.
    they use the '::' operator for change\write the event(the windows messages are events)

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    but what i realy need is these:

    returntype objectname::functionname()//with arguments or not
    That sounds simple enough: returntype would be your callback function (object) type. functionname, which probably should be renamed to something more descriptive, would just return the callback function (object). You can then set the function (object) on a per-object basis outside of the main function, at run time, and then retrieve it from the object named obj by calling obj.functionname(). I don't see a problem.
    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

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    That sounds simple enough: returntype would be your callback function (object) type. functionname, which probably should be renamed to something more descriptive, would just return the callback function (object). You can then set the function (object) on a per-object basis outside of the main function, at run time, and then retrieve it from the object named obj by calling obj.functionname(). I don't see a problem.
    but if i use an objectname, the compiler give me errors
    (i have send you a message, because theres 2 things that i don't know on forum)

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    but if i use an objectname, the compiler give me errors
    Oh right, by objectname you really mean an object's name, not a class named objectname.

    Quote Originally Posted by joaquim
    i think the MFC way, can give you a clue... i think. or the Visual C++ form applications.
    they use the '::' operator for change\write the event(the windows messages are events)
    I am not familiar with MFC or Visual C++ form applications, but here's the thing: why does it matter that in the interface that they provide, the scope resolution operator (i.e., :: ) is used? That's just syntax; it only matters in the bigger picture of the interfaces that they provide. What's more important is what you are trying to do. So if you say that:
    Code:
    but what i realy need is these:
    
    returntype objectname::functionname()//with arguments or not
    then I can tell you that since what you really need is invalid C++, you need to change what you really need, or perhaps recognise that you didn't really need it to begin with. If you cannot do that, then you need to change programming language, or change the programming language.
    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

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    Oh right, by objectname you really mean an object's name, not a class named objectname.


    I am not familiar with MFC or Visual C++ form applications, but here's the thing: why does it matter that in the interface that they provide, the scope resolution operator (i.e., :: ) is used? That's just syntax; it only matters in the bigger picture of the interfaces that they provide. What's more important is what you are trying to do. So if you say that:
    Code:
    but what i realy need is these:
    
    returntype objectname::functionname()//with arguments or not
    then I can tell you that since what you really need is invalid C++, you need to change what you really need, or perhaps recognise that you didn't really need it to begin with. If you cannot do that, then you need to change programming language, or change the programming language.
    what i know that i can do these:
    Code:
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    
    class test
    {
       public:
          event(Print)=[](){;};
    };
    
    void hello()
    {
         cout << "hello world";
    }
    
    int main()
    {
       test a;
       a.print=&hello;
       a.print();
      return 0;
    }
    it's very nice... true, but i must use 'a.print=&hello;'... unless we can do:
    Code:
    #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
    
    class test
    {
       public:
          event(Print)=[](){;};
    };
    
    void a_hello()
    {
         cout << "hello world";
    }
    
    int main()
    {
       test a;
       a.print=&hello;//and the compiler do these line...(it's just a thot)
       a.print();
      return 0;
    }
    what you can tell me about my thot?
    (sorry if i bored you with these thread, but i want something like these)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindWindowEx - By INSTANCE not by name
    By Devils Child in forum C# Programming
    Replies: 5
    Last Post: 02-27-2010, 03:19 AM
  2. How do I access an instance's name?
    By TheodoreG in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2006, 03:25 PM
  3. multiple instance of ADT
    By Kinasz in forum C Programming
    Replies: 4
    Last Post: 04-07-2004, 06:49 AM
  4. Deleted instance
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 05-28-2002, 11:15 AM
  5. How do you only allow one instance of an app?
    By genghis in forum Windows Programming
    Replies: 6
    Last Post: 01-24-2002, 08:31 PM