polymorph object - why doesn't it work???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mika Vainio

    polymorph object - why doesn't it work???

    hi everybody,
    i'm working on the following problem: i need to build a 26-nary tree to save
    a data dictionary. every letter of the words is represented by a cell (cell
    has a pointer-vector cell* children[26]). the last letter is a node
    (node:cell, additional property "pagenr").
    now: i understood the principals of polymorph objects and it worked fine
    with the ususal examples. but in my case it does not! there is no
    compilation or linking error and no error at runtime - at least not shown...
    here's some code. maybe someone can give me some hints...


    // Trie.cpp
    //----------
    (...)
    void Trie::insert(ch ar *word, int page)
    {
    // int value of first letter
    int iword = ((int)word[0])-97;

    insert_one(root->children[iword], word, page);
    }

    void Trie::insert_on e(Cell* new_cell, char *word, int page)
    {
    if (new_cell == NULL) {
    if (((int)word[1])-97 < 0) { // last letter
    new_cell = new Node();

    // new_cell->page = page;
    }
    else
    new_cell = new Cell;

    cout << typeid(new_cell ).name() << " - ";
    new_cell->display(); // virtual function in cell, cout pagenr in node
    (...)

    best regards,
    mika


  • Karthik

    #2
    Re: polymorph object - why doesn't it work???

    Mika Vainio wrote:
    [color=blue]
    > hi everybody,
    > i'm working on the following problem: i need to build a 26-nary tree to save
    > a data dictionary. every letter of the words is represented by a cell (cell
    > has a pointer-vector cell* children[26]). the last letter is a node
    > (node:cell, additional property "pagenr").
    > now: i understood the principals of polymorph objects and it worked fine
    > with the ususal examples. but in my case it does not! there is no
    > compilation or linking error and no error at runtime - at least not shown...
    > here's some code. maybe someone can give me some hints...
    >
    >
    > // Trie.cpp
    > //----------
    > (...)
    > void Trie::insert(ch ar *word, int page)
    > {
    > // int value of first letter
    > int iword = ((int)word[0])-97;[/color]
    Try using macros instead of constants, as in 97 here.[color=blue]
    >
    > insert_one(root->children[iword], word, page);
    > }
    >
    > void Trie::insert_on e(Cell* new_cell, char *word, int page)
    > {
    > if (new_cell == NULL) {
    > if (((int)word[1])-97 < 0) { // last letter
    > new_cell = new Node();
    >
    > // new_cell->page = page;
    > }
    > else
    > new_cell = new Cell;
    >
    > cout << typeid(new_cell ).name() << " - ";
    > new_cell->display(); // virtual function in cell, cout pagenr in node
    > (...)[/color]
    Do you have your own implementation of display function in 'Node'
    class. It would be nice if you can mention the class hierarchy clearly
    out here to understand things better.
    [color=blue]
    >
    > best regards,
    > mika
    >
    >[/color]


    --
    Karthik

    ------

    Human Beings please 'removeme' for my email.

    Comment

    • John Harrison

      #3
      Re: polymorph object - why doesn't it work???


      "Mika Vainio" <mika-spamblock@vaini o.de> wrote in message
      news:c6h8os$2vd $06$1@news.t-online.com...[color=blue]
      > hi everybody,
      > i'm working on the following problem: i need to build a 26-nary tree to[/color]
      save[color=blue]
      > a data dictionary. every letter of the words is represented by a cell[/color]
      (cell[color=blue]
      > has a pointer-vector cell* children[26]). the last letter is a node
      > (node:cell, additional property "pagenr").
      > now: i understood the principals of polymorph objects and it worked fine
      > with the ususal examples. but in my case it does not! there is no
      > compilation or linking error and no error at runtime - at least not[/color]
      shown...[color=blue]
      > here's some code. maybe someone can give me some hints...
      >
      >
      > // Trie.cpp
      > //----------
      > (...)
      > void Trie::insert(ch ar *word, int page)
      > {
      > // int value of first letter
      > int iword = ((int)word[0])-97;
      >
      > insert_one(root->children[iword], word, page);
      > }
      >
      > void Trie::insert_on e(Cell* new_cell, char *word, int page)
      > {
      > if (new_cell == NULL) {
      > if (((int)word[1])-97 < 0) { // last letter
      > new_cell = new Node();
      >
      > // new_cell->page = page;
      > }
      > else
      > new_cell = new Cell;
      >
      > cout << typeid(new_cell ).name() << " - ";
      > new_cell->display(); // virtual function in cell, cout pagenr in node
      > (...)
      >
      > best regards,
      > mika
      >[/color]

      Hard to say from the snippets given, but I would say that the problem is
      incorrect use of pointers not polymorphism.
      [color=blue]
      > insert_one(root->children[iword], word, page);[/color]
      [color=blue]
      > void Trie::insert_on e(Cell* new_cell, char *word, int page)
      > {[/color]

      Think about that code. I think you are assuming that it will write a value
      to root->children[iword] but it doesn't. You need something like this

      root->children[iword] = insert_one(word , page);

      Cell* Trie::insert_on e(char *word, int page)
      {
      ...
      return new_cell;
      }

      john



      Comment

      • Mika Vainio

        #4
        Re: polymorph object - why doesn't it work???

        hi karthik,
        yes, i have my own display() implementation (cout << "Pagenr: " <<
        new_cell->page << endl;). and here are all my header files.
        best regards,
        mika

        // Cell.h
        //-----------------
        class Cell {
        public:
        Cell() {}
        virtual ~Cell();
        virtual void display() const;

        char letter;
        Cell* children[26];
        };


        // Node.h
        //----------------
        class Node : public Cell {
        public:
        Node() {}
        int page;
        void display();
        Node* children[26];
        };


        // Trie.h
        //----------------------------------
        #include "Node.h"

        class Trie {
        public:
        Trie() {
        root = NULL;
        root = new Cell;
        for (int i = 0; i<26; i++)
        root->children[i] = NULL;
        // root->page = 1;
        root->letter = '\0';
        }
        void display();
        void insert(char*, int);
        int search(char*);
        void erase(char*);

        private:
        Cell *root;
        char *word;
        void insert_one(Cell * new_cell, char *word, int page);
        void display_one(Cel l *top, char* prefix);
        int search_one(Cell *&new_cell, char *word);
        void erase_one(Cell *&new_cell, char *word);
        };



        "Karthik" <removeme_kayka ydreamz@yahoo.c om> schrieb im Newsbeitrag
        news:408c2f40$1 @darkstar...[color=blue]
        > Mika Vainio wrote:
        >[color=green]
        > > hi everybody,
        > > i'm working on the following problem: i need to build a 26-nary tree to[/color][/color]
        save[color=blue][color=green]
        > > a data dictionary. every letter of the words is represented by a cell[/color][/color]
        (cell[color=blue][color=green]
        > > has a pointer-vector cell* children[26]). the last letter is a node
        > > (node:cell, additional property "pagenr").
        > > now: i understood the principals of polymorph objects and it worked fine
        > > with the ususal examples. but in my case it does not! there is no
        > > compilation or linking error and no error at runtime - at least not[/color][/color]
        shown...[color=blue][color=green]
        > > here's some code. maybe someone can give me some hints...
        > >
        > >
        > > // Trie.cpp
        > > //----------
        > > (...)
        > > void Trie::insert(ch ar *word, int page)
        > > {
        > > // int value of first letter
        > > int iword = ((int)word[0])-97;[/color]
        > Try using macros instead of constants, as in 97 here.[color=green]
        > >
        > > insert_one(root->children[iword], word, page);
        > > }
        > >
        > > void Trie::insert_on e(Cell* new_cell, char *word, int page)
        > > {
        > > if (new_cell == NULL) {
        > > if (((int)word[1])-97 < 0) { // last letter
        > > new_cell = new Node();
        > >
        > > // new_cell->page = page;
        > > }
        > > else
        > > new_cell = new Cell;
        > >
        > > cout << typeid(new_cell ).name() << " - ";
        > > new_cell->display(); // virtual function in cell, cout pagenr in node
        > > (...)[/color]
        > Do you have your own implementation of display function in 'Node'
        > class. It would be nice if you can mention the class hierarchy clearly
        > out here to understand things better.
        >[color=green]
        > >
        > > best regards,
        > > mika
        > >
        > >[/color]
        >
        >
        > --
        > Karthik
        >
        > ------
        >
        > Human Beings please 'removeme' for my email.[/color]


        Comment

        • Karthik

          #5
          Re: polymorph object - why doesn't it work???

          Mika Vainio wrote:
          [color=blue]
          > hi karthik,
          > yes, i have my own display() implementation (cout << "Pagenr: " <<
          > new_cell->page << endl;). and here are all my header files.
          > best regards,
          > mika
          >
          > // Cell.h
          > //-----------------
          > class Cell {
          > public:
          > Cell() {}
          > virtual ~Cell();
          > virtual void display() const;
          >
          > char letter;
          > Cell* children[26];
          > };
          >
          >
          > // Node.h
          > //----------------
          > class Node : public Cell {
          > public:
          > Node() {}
          > int page;
          > void display();
          > Node* children[26];[/color]

          Whatz this. Why do you need to have an array of 'Cell' pointers ( as
          in the base class) and again 26 pointers again here. I guess thatz
          against polymorphism as such.

          Also the classes storing the data and modelling ought to be separate
          ( decoupled , to get the right term from the literature , that is).

          That means, There might be a hierarchy of data classes, exhitinhg
          polymorphism, but there ought to be a single class (that would have 26
          pointers) that would be used an user to create the tree etc.

          HTH

          Karthik

          ------

          Human Beings please 'removeme' for my email.

          Comment

          • Mika Vainio

            #6
            Re: polymorph object - why doesn't it work???

            hi john,
            i just tried your idea but the last letter is still a cell. this is what i
            wrote:

            void Trie::insert(ch ar *word, int page)
            {
            int iword = ((int)word[0])-97;

            root->children[iword] = insert_one(word , page);
            }

            Cell* Trie::insert_on e(char *word, int page)
            {
            Cell* new_cell = 0;

            if (((int)word[1])-97 < 0) { // Node anlegen
            new_cell = new Node();
            cout << typeid(new_cell ).name() << endl;
            new_cell->display();
            }
            else
            new_cell = new Cell;

            for (int i = 0; i<26; i++)
            new_cell->children[i] = NULL;

            new_cell->letter = word[0];

            int iword = ((int)word[1])-97;
            if (iword >= 0) {
            new_cell->children[iword] = insert_one(word +1, page);
            }

            return new_cell;
            }

            output: class Cell *

            best regards,
            mika


            Comment

            • Mika Vainio

              #7
              Re: polymorph object - why doesn't it work???

              ops, you're right - this comes from an earlier version where i used node for
              all letters. i killed this line but that didn't change anything - the last
              letter is still a cell...
              and did i understand you correctly: the pointer vector should be changed
              into 26 single cell pointers in a separate class?
              best regards,
              mika


              "Karthik" <removeme_kayka ydreamz@yahoo.c om> schrieb im Newsbeitrag
              news:408c473b$1 @darkstar...[color=blue]
              > Mika Vainio wrote:
              >[color=green]
              > > hi karthik,
              > > yes, i have my own display() implementation (cout << "Pagenr: " <<
              > > new_cell->page << endl;). and here are all my header files.
              > > best regards,
              > > mika
              > >
              > > // Cell.h
              > > //-----------------
              > > class Cell {
              > > public:
              > > Cell() {}
              > > virtual ~Cell();
              > > virtual void display() const;
              > >
              > > char letter;
              > > Cell* children[26];
              > > };
              > >
              > >
              > > // Node.h
              > > //----------------
              > > class Node : public Cell {
              > > public:
              > > Node() {}
              > > int page;
              > > void display();
              > > Node* children[26];[/color]
              >
              > Whatz this. Why do you need to have an array of 'Cell' pointers ( as
              > in the base class) and again 26 pointers again here. I guess thatz
              > against polymorphism as such.
              >
              > Also the classes storing the data and modelling ought to be separate
              > ( decoupled , to get the right term from the literature , that is).
              >
              > That means, There might be a hierarchy of data classes, exhitinhg
              > polymorphism, but there ought to be a single class (that would have 26
              > pointers) that would be used an user to create the tree etc.
              >
              > HTH
              >
              > Karthik
              >
              > ------
              >
              > Human Beings please 'removeme' for my email.[/color]


              Comment

              • John Harrison

                #8
                Re: polymorph object - why doesn't it work???


                "Mika Vainio" <mika-spamblock@vaini o.de> wrote in message
                news:c6hisc$dvr $02$1@news.t-online.com...[color=blue]
                > hi john,
                > i just tried your idea but the last letter is still a cell. this is what i
                > wrote:
                >[/color]

                I think I'm going to have to see a complete compilable program to help with
                this one.

                john


                Comment

                • Iulian Dragos

                  #9
                  Re: polymorph object - why doesn't it work???

                  Mika Vainio wrote:[color=blue]
                  > Cell* Trie::insert_on e(char *word, int page)
                  > {
                  > Cell* new_cell = 0;
                  >
                  > if (((int)word[1])-97 < 0) { // Node anlegen
                  > new_cell = new Node();
                  > cout << typeid(new_cell ).name() << endl;[/color]

                  Here use "cout << typeid(*new_cel l).name() << endl; instead of
                  typeid(new_cell ). Whithout dereferencing the pointer, you get the type
                  of the *pointer*, which is correct (Cell*).
                  [color=blue]
                  > new_cell->display();[/color]

                  which display() method is called? the one in Cell, or the one in Node. I
                  prefer printing a text in the derived method than using typeid...

                  hth,
                  Iuli

                  Comment

                  • Uwe Schnitker

                    #10
                    Re: polymorph object - why doesn't it work???

                    <original message snipped to pieces>[color=blue]
                    >
                    > // Cell.h
                    > //-----------------
                    > class Cell {
                    > public:
                    > Cell() {}
                    > virtual ~Cell();
                    > virtual void display() const;[/color]

                    Look at this declaration of display,
                    [color=blue]
                    > char letter;
                    > Cell* children[26];
                    > };
                    >
                    >
                    > // Node.h
                    > //----------------
                    > class Node : public Cell {
                    > public:
                    > Node() {}
                    > int page;
                    > void display();[/color]

                    and compare with this one, please.
                    [color=blue]
                    > Node* children[26];
                    > };[/color]

                    You will see that the virtual function in the base class is a const one,
                    but the function declared in the derived class is not. Since this is C++,
                    not Java, the declaration in the derived class doesn't override that in the
                    base class, but rather hides it.

                    To take part in dynamic dispatch, the function in the derived class must have
                    exactly the same signature as the base class function. (Apart from the return
                    type, wich can be of a derived type if your compiler indeed supports "covariant
                    returns".)

                    HTH,

                    Uwe

                    Comment

                    Working...