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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ugly cast

Ugly cast

Scheduled Pinned Locked Moved C / C++ / MFC
question
21 Posts 7 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.
  • _ Offline
    _ Offline
    _Magnus_
    wrote on last edited by
    #1

    class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


    - I don't necessarily agree with everything I say

    J T T N P 5 Replies Last reply
    0
    • _ _Magnus_

      class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


      - I don't necessarily agree with everything I say

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      It is valid Inc uses i of B inherited from A When u cast the pointer, pb has all the class info of B, he can find Inc (which is logic :) ) and can find i, so no problem Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      1 Reply Last reply
      0
      • _ _Magnus_

        class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


        - I don't necessarily agree with everything I say

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        yes it works, and you're not even obliged to cast into (B*). the pointer is a base class pointer and it calls the B::Inc() function. you could do much interresting, with creating another Inc() function into class A... what happens now? A::Inc() is called. strange ? no. it is a well known consequence of virtual keyword. when you have many derives classes (as here), and into which many functions have the same name, you'll have to declare virtual (in the base class) the function if you want with such command lines that the B::Inc() is called. in brief, :

        without virtual :
        class A {
        protected:
        int i;
        public:
        void Inc (void);
        };
        class B : public class A {
        public:
        void Inc (void);
        };
        A* pa = new(B);
        pa->Inc(); //A::Inc() called

        with virtual :
        class A {
        protected:
        int i;
        public:
        virtual void Inc (void);
        };
        class B : public class A {
        public:
        void Inc (void);
        };
        A* pa = new(B);
        pa->Inc(); //B::Inc() called

        ps: why do you put your int i; into public statement ?


        TOXCCT >>> GEII power

        _ 1 Reply Last reply
        0
        • T toxcct

          yes it works, and you're not even obliged to cast into (B*). the pointer is a base class pointer and it calls the B::Inc() function. you could do much interresting, with creating another Inc() function into class A... what happens now? A::Inc() is called. strange ? no. it is a well known consequence of virtual keyword. when you have many derives classes (as here), and into which many functions have the same name, you'll have to declare virtual (in the base class) the function if you want with such command lines that the B::Inc() is called. in brief, :

          without virtual :
          class A {
          protected:
          int i;
          public:
          void Inc (void);
          };
          class B : public class A {
          public:
          void Inc (void);
          };
          A* pa = new(B);
          pa->Inc(); //A::Inc() called

          with virtual :
          class A {
          protected:
          int i;
          public:
          virtual void Inc (void);
          };
          class B : public class A {
          public:
          void Inc (void);
          };
          A* pa = new(B);
          pa->Inc(); //B::Inc() called

          ps: why do you put your int i; into public statement ?


          TOXCCT >>> GEII power

          _ Offline
          _ Offline
          _Magnus_
          wrote on last edited by
          #4

          Yes, i know how virtual baseclasses work, what i was wondering was about casting to a subclass with no data from a baseclass. toxcct wrote: ps: why do you put your int i; into public statement ? I did it so a could access it from B and i dont like the protected keyword..=) /Magnus


          - I don't necessarily agree with everything I say

          T T 2 Replies Last reply
          0
          • _ _Magnus_

            class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


            - I don't necessarily agree with everything I say

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            It works but it is very very bad (and wrong). What if B had another variable J and a routine IncJ? If you invoked IncJ, you would be trashing memory and could cause your program to crash. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            _ 1 Reply Last reply
            0
            • _ _Magnus_

              Yes, i know how virtual baseclasses work, what i was wondering was about casting to a subclass with no data from a baseclass. toxcct wrote: ps: why do you put your int i; into public statement ? I did it so a could access it from B and i dont like the protected keyword..=) /Magnus


              - I don't necessarily agree with everything I say

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              oh :wtf: you should ;P because there is no protection on your i member, and so anybody can modify it ... you agree so that there is no interrest ;)


              TOXCCT >>> GEII power

              T _ 2 Replies Last reply
              0
              • _ _Magnus_

                Yes, i know how virtual baseclasses work, what i was wondering was about casting to a subclass with no data from a baseclass. toxcct wrote: ps: why do you put your int i; into public statement ? I did it so a could access it from B and i dont like the protected keyword..=) /Magnus


                - I don't necessarily agree with everything I say

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                What you are doing will work then, but it is really bad code. Sometimes people do what you do when they have no choice and need to access protected and private members in a given class. This is usually do to poor design of the base class. (*cough* BORLAND VCL) However, what you are doing is very questionable indeed. It looks like a maintenance nightmare. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                1 Reply Last reply
                0
                • T toxcct

                  oh :wtf: you should ;P because there is no protection on your i member, and so anybody can modify it ... you agree so that there is no interrest ;)


                  TOXCCT >>> GEII power

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  There are many cases where a member can be public without issue. Look at the CRect class. What would be the point of protecting left, top, right and bottom? Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  T 1 Reply Last reply
                  0
                  • T Tim Smith

                    There are many cases where a member can be public without issue. Look at the CRect class. What would be the point of protecting left, top, right and bottom? Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    why do we protect data members ?


                    TOXCCT >>> GEII power

                    T 1 Reply Last reply
                    0
                    • T Tim Smith

                      It works but it is very very bad (and wrong). What if B had another variable J and a routine IncJ? If you invoked IncJ, you would be trashing memory and could cause your program to crash. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                      _ Offline
                      _ Offline
                      _Magnus_
                      wrote on last edited by
                      #10

                      Yes i know its really ugly but i managed to do it by misstake and got a bit surprised that it worked. =) /Magnus


                      - I don't necessarily agree with everything I say

                      1 Reply Last reply
                      0
                      • T toxcct

                        oh :wtf: you should ;P because there is no protection on your i member, and so anybody can modify it ... you agree so that there is no interrest ;)


                        TOXCCT >>> GEII power

                        _ Offline
                        _ Offline
                        _Magnus_
                        wrote on last edited by
                        #11

                        I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus


                        - I don't necessarily agree with everything I say

                        T T G 3 Replies Last reply
                        0
                        • _ _Magnus_

                          I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus


                          - I don't necessarily agree with everything I say

                          T Offline
                          T Offline
                          toxcct
                          wrote on last edited by
                          #12

                          no, a protected member is only modifiable by the derived classes objects (and of course objects from the class itself), and occasionately by friends functions too, but in any case by the outside.


                          TOXCCT >>> GEII power

                          _ 1 Reply Last reply
                          0
                          • T toxcct

                            no, a protected member is only modifiable by the derived classes objects (and of course objects from the class itself), and occasionately by friends functions too, but in any case by the outside.


                            TOXCCT >>> GEII power

                            _ Offline
                            _ Offline
                            _Magnus_
                            wrote on last edited by
                            #13

                            Yes i know how it works i just dont see the point with the protected keyword. /Magnus


                            - I don't necessarily agree with everything I say

                            T 1 Reply Last reply
                            0
                            • _ _Magnus_

                              Yes i know how it works i just dont see the point with the protected keyword. /Magnus


                              - I don't necessarily agree with everything I say

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #14

                              _Magnus_ wrote: i dont see the point sorry, my english is poor sometimes :-D what do you mean with this ?


                              TOXCCT >>> GEII power

                              1 Reply Last reply
                              0
                              • _ _Magnus_

                                class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


                                - I don't necessarily agree with everything I say

                                N Offline
                                N Offline
                                Navin
                                wrote on last edited by
                                #15

                                IT may work for your particular sutiation... but the proper way of casting in this sense is with dynamic_cast:A *pa = new A; B *pb = dynamic_cast<B *>(pa);
                                pb will either contain a valid pointer to a B object, or will be NULL if it can't be casted safely. Sometimes I feel like I'm a USB printer in a parallel universe.

                                1 Reply Last reply
                                0
                                • _ _Magnus_

                                  class A { public: A() : i(0) { } int i; }; class B : public A { public: void Inc() { i++; } }; A *pa = new A; B *pb = (B *)pa; pb->Inc(); The pointer is not a type B, but class B has no own data and if i run it it works. Is this valid? What happens when i do that? /Magnus


                                  - I don't necessarily agree with everything I say

                                  P Offline
                                  P Offline
                                  Paul Ranson
                                  wrote on last edited by
                                  #16

                                  It works by accident. It's why you should never use the old style C cast. This compiles,

                                  B * pb = (B*)0;
                                  pb->Inc () ;

                                  but goes bang. Paul

                                  T 1 Reply Last reply
                                  0
                                  • P Paul Ranson

                                    It works by accident. It's why you should never use the old style C cast. This compiles,

                                    B * pb = (B*)0;
                                    pb->Inc () ;

                                    but goes bang. Paul

                                    T Offline
                                    T Offline
                                    Tim Smith
                                    wrote on last edited by
                                    #17

                                    Hmm B * pc = reinterpret_cast < B *> (0); This compiles too, so does that mean you should never use it either? Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                    P 1 Reply Last reply
                                    0
                                    • T toxcct

                                      why do we protect data members ?


                                      TOXCCT >>> GEII power

                                      T Offline
                                      T Offline
                                      Tim Smith
                                      wrote on last edited by
                                      #18

                                      I am not saying you shouldn't, I am saying you don't have to. This isn't a black/white issue. It isn't about always doing it or never doing it. It is about doing it when it is appropriate. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                      1 Reply Last reply
                                      0
                                      • _ _Magnus_

                                        I dont really see the point with the protected keyword. Maybe someone inherits from the class and then it can be modified, either it is publicly modifiable or not, i dont see the point with have it "maybemodifable". /Magnus


                                        - I don't necessarily agree with everything I say

                                        T Offline
                                        T Offline
                                        Tim Smith
                                        wrote on last edited by
                                        #19

                                        The protected keyword is for people who trust others to properly augment the implementation of a class in a derived class. In the last three or four weeks I have wasted endless amounts of time because VCL (Borland's MFC) made far too many things private. I couldn't fix the bugs in their code without completely replacing their classes. If they had made their stuff protected and trusted me, I would have saved a lot of time. I hardly ever use the private keyword because I trust the programmers not to screw up the implantation. If they do screw things up, I beat them up and then make them fix it. I would rather them do that than have to reimplement my classes in their own stuff creating two implementations of the same theme. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                        1 Reply Last reply
                                        0
                                        • T Tim Smith

                                          Hmm B * pc = reinterpret_cast < B *> (0); This compiles too, so does that mean you should never use it either? Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                          P Offline
                                          P Offline
                                          Paul Ranson
                                          wrote on last edited by
                                          #20

                                          reinterpret_cast stands out like a sore thumb whereas the C style one doesn't. In either case you can blow your foot off by demanding the compilation of something absurd, like the original poster. Paul

                                          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