Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How does this work?

How does this work?

Scheduled Pinned Locked Moved Managed C++/CLI
question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LiquidE_SA
    wrote on last edited by
    #1

    Can someone please tell me how the following is possible? How is it possible to cast a LeafTrieNode pointer to a "NonLeafTrieNode" pointer, like this? p->ptrs[pos] = (NonLeafTrieNode*) new LeafTrieNode(suffix); The program works like this, but I don't know how it works, because "LeafTrieNode" isn't derived from "NonLeafTrieNode" "lieaftrienode.h" #ifndef LEAF_TRIE_NODE_H #define LEAF_TRIE_NODE_H class LeafTrieNode { public: LeafTrieNode(){}; LeafTrieNode(char*); private: bool leaf; char *word; friend class Trie; }; #endif "nonlieaftrienode.h" #ifndef NON_LEAF_TRIE_NODE_H #define NON_LEAF_TRIE_NODE_H class NonLeafTrieNode { public: NonLeafTrieNode(){}; NonLeafTrieNode(char); private: friend class Trie; NonLeafTrieNode **ptrs; char *letters; bool isLeaf; bool endOfWord; }; #endif -- modified at 9:54 Monday 19th September, 2005

    R V J 3 Replies Last reply
    0
    • L LiquidE_SA

      Can someone please tell me how the following is possible? How is it possible to cast a LeafTrieNode pointer to a "NonLeafTrieNode" pointer, like this? p->ptrs[pos] = (NonLeafTrieNode*) new LeafTrieNode(suffix); The program works like this, but I don't know how it works, because "LeafTrieNode" isn't derived from "NonLeafTrieNode" "lieaftrienode.h" #ifndef LEAF_TRIE_NODE_H #define LEAF_TRIE_NODE_H class LeafTrieNode { public: LeafTrieNode(){}; LeafTrieNode(char*); private: bool leaf; char *word; friend class Trie; }; #endif "nonlieaftrienode.h" #ifndef NON_LEAF_TRIE_NODE_H #define NON_LEAF_TRIE_NODE_H class NonLeafTrieNode { public: NonLeafTrieNode(){}; NonLeafTrieNode(char); private: friend class Trie; NonLeafTrieNode **ptrs; char *letters; bool isLeaf; bool endOfWord; }; #endif -- modified at 9:54 Monday 19th September, 2005

      R Offline
      R Offline
      RichardS
      wrote on last edited by
      #2

      Hi, Simple answer is the same way you can cast anything else with a pointer. A pointer just points to memory, hence it can be made to "think" that it is pointing to a different object type. The new creates a pointer of type LeafTrieNode, and the cast works because you are allowed to change a pointers type. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

      V 1 Reply Last reply
      0
      • L LiquidE_SA

        Can someone please tell me how the following is possible? How is it possible to cast a LeafTrieNode pointer to a "NonLeafTrieNode" pointer, like this? p->ptrs[pos] = (NonLeafTrieNode*) new LeafTrieNode(suffix); The program works like this, but I don't know how it works, because "LeafTrieNode" isn't derived from "NonLeafTrieNode" "lieaftrienode.h" #ifndef LEAF_TRIE_NODE_H #define LEAF_TRIE_NODE_H class LeafTrieNode { public: LeafTrieNode(){}; LeafTrieNode(char*); private: bool leaf; char *word; friend class Trie; }; #endif "nonlieaftrienode.h" #ifndef NON_LEAF_TRIE_NODE_H #define NON_LEAF_TRIE_NODE_H class NonLeafTrieNode { public: NonLeafTrieNode(){}; NonLeafTrieNode(char); private: friend class Trie; NonLeafTrieNode **ptrs; char *letters; bool isLeaf; bool endOfWord; }; #endif -- modified at 9:54 Monday 19th September, 2005

        V Offline
        V Offline
        VaporTrace
        wrote on last edited by
        #3

        :confused: I can't see that it would with what you have displayed because there's alot missing to a completed and running example. What exactly is p-> ? If p is a member of a child class of both or a trianglular reference to a void parent member not listed, I could see where your compiler may confused. Either way, it looks like you are opening up to a memory leak.

        1 Reply Last reply
        0
        • R RichardS

          Hi, Simple answer is the same way you can cast anything else with a pointer. A pointer just points to memory, hence it can be made to "think" that it is pointing to a different object type. The new creates a pointer of type LeafTrieNode, and the cast works because you are allowed to change a pointers type. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

          V Offline
          V Offline
          VaporTrace
          wrote on last edited by
          #4

          Hi Rich, I'm just pondering how his compiler is allowing the cross reference from one object pointer to the other unless p-> is of void that, yes just points to raw memory space by which he will need to not only delete the pointer but reclaim the memory space as well or get a memory leak. :doh:

          R 1 Reply Last reply
          0
          • V VaporTrace

            Hi Rich, I'm just pondering how his compiler is allowing the cross reference from one object pointer to the other unless p-> is of void that, yes just points to raw memory space by which he will need to not only delete the pointer but reclaim the memory space as well or get a memory leak. :doh:

            R Offline
            R Offline
            RichardS
            wrote on last edited by
            #5

            Hi VaporTrace, It is just a cast. Any pointer can be cast into any other pointer. The fact that they are unrelated is the programmers fault. Yes you would get a memory leak if you deleted the pointer as is, but the programmer caused the problem not the compiler. Take the situation:

            class A {};
            class B {};
            A *castme = (A *) new B();

            The compiler just makes castme point to the memory irrespective of the pointers original type. Remember C/C++ is not type safe so you are allowed to do things like this. Ask yourself how void pointers are created. They need to be a pointer of some type before they become a void pointer; you would have had to cast it to a void pointer first. Hope this explains it better. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

            V 1 Reply Last reply
            0
            • R RichardS

              Hi VaporTrace, It is just a cast. Any pointer can be cast into any other pointer. The fact that they are unrelated is the programmers fault. Yes you would get a memory leak if you deleted the pointer as is, but the programmer caused the problem not the compiler. Take the situation:

              class A {};
              class B {};
              A *castme = (A *) new B();

              The compiler just makes castme point to the memory irrespective of the pointers original type. Remember C/C++ is not type safe so you are allowed to do things like this. Ask yourself how void pointers are created. They need to be a pointer of some type before they become a void pointer; you would have had to cast it to a void pointer first. Hope this explains it better. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

              V Offline
              V Offline
              VaporTrace
              wrote on last edited by
              #6

              Duh yup, When you simplify, it's painfully clear as A* = A* to new object as: NonLeafTrieNode **ptrs = (NonLeafTrieNode*) new object; I think you know what I'm trying to say rather that ptrs[x]. That this pointer -> just kept saying "I'm in a mystery object" which doesn't matter. Err. Hate it when that happens. :doh: -- modified at 1:50 Tuesday 20th September, 2005 :suss: Ya know this dumb thing kept buggin me so I went to my compiler and guess what? It worked! But it still kept bugging me since this is not a normal thing I would try so I thought outside of the obvious why havn't I tried this just for the stupidity of it. Then I thought. :cool: class A{}; class B{ public: int j; }; int main (int argc, char* argv[]){ A* newptr = (A*) new B; newptr->j=5; return 0; } ;P Ya, I was a bit suprised when it let me do the initial A* newptr = (A*) new B; because it just doesn't feel right. Guess I'm too methodical.

              R 1 Reply Last reply
              0
              • V VaporTrace

                Duh yup, When you simplify, it's painfully clear as A* = A* to new object as: NonLeafTrieNode **ptrs = (NonLeafTrieNode*) new object; I think you know what I'm trying to say rather that ptrs[x]. That this pointer -> just kept saying "I'm in a mystery object" which doesn't matter. Err. Hate it when that happens. :doh: -- modified at 1:50 Tuesday 20th September, 2005 :suss: Ya know this dumb thing kept buggin me so I went to my compiler and guess what? It worked! But it still kept bugging me since this is not a normal thing I would try so I thought outside of the obvious why havn't I tried this just for the stupidity of it. Then I thought. :cool: class A{}; class B{ public: int j; }; int main (int argc, char* argv[]){ A* newptr = (A*) new B; newptr->j=5; return 0; } ;P Ya, I was a bit suprised when it let me do the initial A* newptr = (A*) new B; because it just doesn't feel right. Guess I'm too methodical.

                R Offline
                R Offline
                RichardS
                wrote on last edited by
                #7

                But you will probably find that you corrupted memory by doing the newptr->j=5; I agree this is not normal programming practice and no-one really does it unless there is a very good reason. "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                1 Reply Last reply
                0
                • L LiquidE_SA

                  Can someone please tell me how the following is possible? How is it possible to cast a LeafTrieNode pointer to a "NonLeafTrieNode" pointer, like this? p->ptrs[pos] = (NonLeafTrieNode*) new LeafTrieNode(suffix); The program works like this, but I don't know how it works, because "LeafTrieNode" isn't derived from "NonLeafTrieNode" "lieaftrienode.h" #ifndef LEAF_TRIE_NODE_H #define LEAF_TRIE_NODE_H class LeafTrieNode { public: LeafTrieNode(){}; LeafTrieNode(char*); private: bool leaf; char *word; friend class Trie; }; #endif "nonlieaftrienode.h" #ifndef NON_LEAF_TRIE_NODE_H #define NON_LEAF_TRIE_NODE_H class NonLeafTrieNode { public: NonLeafTrieNode(){}; NonLeafTrieNode(char); private: friend class Trie; NonLeafTrieNode **ptrs; char *letters; bool isLeaf; bool endOfWord; }; #endif -- modified at 9:54 Monday 19th September, 2005

                  J Offline
                  J Offline
                  Johann Gerell
                  wrote on last edited by
                  #8

                  A general observation: Don't use C-style casts for non-PODs in C++. They make it far too easy to cast a mountain to hollandaise sauce. -- The Blog: Bits and Pieces -- modified at 1:55 Wednesday 21st September, 2005

                  R 1 Reply Last reply
                  0
                  • J Johann Gerell

                    A general observation: Don't use C-style casts for non-PODs in C++. They make it far too easy to cast a mountain to hollandaise sauce. -- The Blog: Bits and Pieces -- modified at 1:55 Wednesday 21st September, 2005

                    R Offline
                    R Offline
                    RichardS
                    wrote on last edited by
                    #9

                    By the by, Johann, what is a POD?? "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                    J 1 Reply Last reply
                    0
                    • R RichardS

                      By the by, Johann, what is a POD?? "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                      J Offline
                      J Offline
                      Johann Gerell
                      wrote on last edited by
                      #10

                      Oh, sorry. POD = Plain Ol' Data, ints, doubles and such goodies. -- The Blog: Bits and Pieces -- modified at 17:18 Saturday 24th September, 2005

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups