Help! dynamic classes (shapeshifter)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Timothy Stark

    Help! dynamic classes (shapeshifter)

    Hello folks,

    I am working on my program and was trying to implement 'dynamic' classes
    by using virtual function calls but it did not work. I want
    to convert from 'class test1' to 'class test2' without affecting
    stored variables much like class shapeshifter. That's why I need
    that for my own socket interface. Also I want to print "Test #2 (1)"
    from 'b'. Instead it prints "Test #1 (1)".

    For example,

    a = new test1;
    b = (test2 *)a;
    c = new test2;

    It results: (this program prints that:)

    Test #1 (1)
    Test #1 (1)
    Test #2 (2)

    Here is my test program.

    #include <stdio.h>

    class socket {
    public:
    int idSocket;

    virtual void print(void) {}
    };

    class test1 : public socket
    {
    public:
    void print(void)
    {
    printf("Test #1 (%d)\n", idSocket);
    }
    };

    class test2 : public socket
    {
    public:
    void print(void)
    {
    printf("Test #2 (%d)\n", idSocket);
    }
    };

    int main(void)
    {
    test1 *a;
    test2 *b;
    test2 *c;

    a = new test1;
    a->idSocket = 1;
    b = (test2 *)a;
    c = new test2;
    c->idSocket = 2;

    a->print();
    b->print();
    c->print();

    return 0;
    }

    Thank you!
    Tim


  • Ron Natalie

    #2
    Re: Help! dynamic classes (shapeshifter)


    "Timothy Stark" <sword7nospam@s peakeasy.org> wrote in message news:EdadnUVRqo ZOiTqiRTvUqQ@sp eakeasy.net...
    [color=blue]
    > a = new test1;
    > b = (test2 *)a;[/color]

    This is bogus. You can't convert test1 to test2 sanely. The C-style cast
    in this case ends up being a reinterpret_cas t.

    Just what are you trying to accomplish? The normal paradigm is to
    convert test1 and test2 instances to their common base (socket).


    Comment

    • Timothy Stark

      #3
      Re: Help! dynamic classes (shapeshifter)


      "Ron Natalie" <ron@sensor.com > wrote in message
      news:3fa6d1cf$0 $218$9a6e19ea@n ews.newshosting .com...[color=blue]
      > This is bogus. You can't convert test1 to test2 sanely. The C-style[/color]
      cast[color=blue]
      > in this case ends up being a reinterpret_cas t.[/color]

      Oh, well. Thank you. I tried that and it did not work. I have to look for
      different method
      to use dynamic function calls.
      [color=blue]
      > Just what are you trying to accomplish? The normal paradigm is to
      > convert test1 and test2 instances to their common base (socket).[/color]

      In my program, I want to use simple dynamic function call (callback
      function) to the specific process routine from handle routine (incoming data
      from TCP/IP network). I would be consider 'this->*' method. For example,
      there are two different classes that inherit to that same socket class.
      Will 'this->*' work on different class that inherit the socket class? How
      about dynamic virtual function calls, etc? I am looking for some methods
      because I am new to C++ programming.

      class Socket {
      public:
      int idSocket;
      void (*process)(char *, int);

      static void Handle(void);

      inline void SetCallback(voi d (*newcall)(char *, int))
      { process = newcall; }
      };

      class test1 : public Socket
      {
      :
      void process2(char *, int);
      void process1(char *, int);
      };

      class test2 : public Socket
      {
      :
      void process3(char *, int);
      void process4(char *, int);
      };

      void Socket::Handle( void)
      {
      Socket *sck;

      :
      :
      len = read(idSocket, data, sizeof(data));
      (sck->*process)(data , len);
      }

      Thank you!
      Tim


      Comment

      Working...