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. Casting from within a class

Casting from within a class

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancelearningworkspace
4 Posts 3 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.
  • G Offline
    G Offline
    Greven
    wrote on last edited by
    #1

    Is there an easy way to get a subclass out of a class from within the class? This would be easiest explained in code ;P class A; class B: public A; From within B, I need to get a temporary variable of the subclass A. The subclass is setup with void operator=(A& a); None of them are pointers, just ... can't remember the term right now... I access them with a . not a -> man my memory is going... Anyway, I've tried this: A a; a = (A)this; and it of course doesn't work. How can I grab the data from the subclass? What I'm doing is implementing a rotation function for the nice CPolygon class that Chris wrote (*very* nice class btw). And I want to make a temp copy of the CPolygon data so I don't loose the data as the poly rotates. (If you rotate a polygon to much it colapses in on itself due to inaccuracy) So I want to make a backup copy of the data, rotate that data and then use it. But I can't grab the data... What do I do?!? Argh! Programming in binary is as easy as 01 10 11.

    M 1 Reply Last reply
    0
    • G Greven

      Is there an easy way to get a subclass out of a class from within the class? This would be easiest explained in code ;P class A; class B: public A; From within B, I need to get a temporary variable of the subclass A. The subclass is setup with void operator=(A& a); None of them are pointers, just ... can't remember the term right now... I access them with a . not a -> man my memory is going... Anyway, I've tried this: A a; a = (A)this; and it of course doesn't work. How can I grab the data from the subclass? What I'm doing is implementing a rotation function for the nice CPolygon class that Chris wrote (*very* nice class btw). And I want to make a temp copy of the CPolygon data so I don't loose the data as the poly rotates. (If you rotate a polygon to much it colapses in on itself due to inaccuracy) So I want to make a backup copy of the data, rotate that data and then use it. But I can't grab the data... What do I do?!? Argh! Programming in binary is as easy as 01 10 11.

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:

      void B::SomeFunc()
      {
      A& refa = *(A*) this;

      // use refa.whatever() here...
      }

      --Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

      G T 2 Replies Last reply
      0
      • M Michael Dunn

        A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:

        void B::SomeFunc()
        {
        A& refa = *(A*) this;

        // use refa.whatever() here...
        }

        --Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

        G Offline
        G Offline
        Greven
        wrote on last edited by
        #3

        Worked like a charm! Thanks a lot. Why does the syntax have to be so confusing sometimes :) Programming in binary is as easy as 01 10 11.

        1 Reply Last reply
        0
        • M Michael Dunn

          A is the parent class of B, not the subclass. A pointer to B can always be cast to a pointer to A, so just do:

          void B::SomeFunc()
          {
          A& refa = *(A*) this;

          // use refa.whatever() here...
          }

          --Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.

          T Offline
          T Offline
          Todd Smith
          wrote on last edited by
          #4

          this would have worked.

          A a;
          a = *this;

          Todd Smith

          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