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. This 'works' but would you ever want to ??

This 'works' but would you ever want to ??

Scheduled Pinned Locked Moved C / C++ / MFC
questionjava
6 Posts 2 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.
  • R Offline
    R Offline
    RedZenBird
    wrote on last edited by
    #1

    The question came up today in our shop "can I call one constructor from another just like I can in java" and 'out of curiousity' tried to figure out if it can be done...After a few attempts (at what I thought were direct 'and sensible' approaches that only made the compiler whine) I came up with the following....Now, don't get me wrong, I'm not advocating that anyone *should* do this....only that it appears that you can and that it looks like it 'works'....:-D:-D:-D #include "stdafx.h" #include "stdlib.h" int flag; class CtorTest { public: CtorTest( void ) { int x = 1; } CtorTest( int iFoo ) { new( this ) CtorTest; int x = 2;; } ~CtorTest( void ) { int x = 1; } void operator delete(void* p, CtorTest* poOuter) { free( poOuter ); } void* operator new(size_t s, CtorTest* poOuter ) { return poOuter; } }; int main(int argc, char* argv[]) { printf("Hello World!\n"); int aNum = 5; CtorTest x( aNum ); return 0; } Just trying to keep the forces of entropy at bay

    R J 2 Replies Last reply
    0
    • R RedZenBird

      The question came up today in our shop "can I call one constructor from another just like I can in java" and 'out of curiousity' tried to figure out if it can be done...After a few attempts (at what I thought were direct 'and sensible' approaches that only made the compiler whine) I came up with the following....Now, don't get me wrong, I'm not advocating that anyone *should* do this....only that it appears that you can and that it looks like it 'works'....:-D:-D:-D #include "stdafx.h" #include "stdlib.h" int flag; class CtorTest { public: CtorTest( void ) { int x = 1; } CtorTest( int iFoo ) { new( this ) CtorTest; int x = 2;; } ~CtorTest( void ) { int x = 1; } void operator delete(void* p, CtorTest* poOuter) { free( poOuter ); } void* operator new(size_t s, CtorTest* poOuter ) { return poOuter; } }; int main(int argc, char* argv[]) { printf("Hello World!\n"); int aNum = 5; CtorTest x( aNum ); return 0; } Just trying to keep the forces of entropy at bay

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

      okay, thought a little explanation is due here as well.....in main() we get a CtorTest object and the CtorTest::CtorTest(int) constructor gets called. Then we jump into the placement new function that effectively returns 'this'....Then from that point, we jump into the CtorTest::CtorTest(void) function ( invoked right after the return from the local placement new function. After that, control returns to the 'first constructor' call and we return to main. To support the assertion that there is only one CtorTest object created (?), the destructor is only called once......strange...very strange... Just trying to keep the forces of entropy at bay

      1 Reply Last reply
      0
      • R RedZenBird

        The question came up today in our shop "can I call one constructor from another just like I can in java" and 'out of curiousity' tried to figure out if it can be done...After a few attempts (at what I thought were direct 'and sensible' approaches that only made the compiler whine) I came up with the following....Now, don't get me wrong, I'm not advocating that anyone *should* do this....only that it appears that you can and that it looks like it 'works'....:-D:-D:-D #include "stdafx.h" #include "stdlib.h" int flag; class CtorTest { public: CtorTest( void ) { int x = 1; } CtorTest( int iFoo ) { new( this ) CtorTest; int x = 2;; } ~CtorTest( void ) { int x = 1; } void operator delete(void* p, CtorTest* poOuter) { free( poOuter ); } void* operator new(size_t s, CtorTest* poOuter ) { return poOuter; } }; int main(int argc, char* argv[]) { printf("Hello World!\n"); int aNum = 5; CtorTest x( aNum ); return 0; } Just trying to keep the forces of entropy at bay

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        Sure, why not? It's perfectly legal to do replacement new like that. However, I'm sure it would be frowned upon in a programming team. ;) One way to do it with a more java-like syntax would be:

        template <typename T>
        class java_lookalike_helper {
        protected:
        typedef T this_t;
        };

        #define this_ new (this) this_t

        class Class : java_lookalike_helper<Class> {
        int x, int y;

        public:
        Class(int x_) : x(x_) {
        }

        Class(int x\_, int y\_) : y(\_y) {
            this\_(x\_);  // Check out the spiffy java like syntax!
        }
        

        };

        -- Please state the nature of your medical emergency.

        R 1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          Sure, why not? It's perfectly legal to do replacement new like that. However, I'm sure it would be frowned upon in a programming team. ;) One way to do it with a more java-like syntax would be:

          template <typename T>
          class java_lookalike_helper {
          protected:
          typedef T this_t;
          };

          #define this_ new (this) this_t

          class Class : java_lookalike_helper<Class> {
          int x, int y;

          public:
          Class(int x_) : x(x_) {
          }

          Class(int x\_, int y\_) : y(\_y) {
              this\_(x\_);  // Check out the spiffy java like syntax!
          }
          

          };

          -- Please state the nature of your medical emergency.

          R Offline
          R Offline
          RedZenBird
          wrote on last edited by
          #4

          thanx for that variant of this idea. I'll add that to the 'tool kit' and play around with that as well.....this arcane-chicanery must be what causes some advocates to lean toward the 'managed code' camp...where we likely can't have this kind of 'fun' with the language :-D;) Just trying to keep the forces of entropy at bay

          J 1 Reply Last reply
          0
          • R RedZenBird

            thanx for that variant of this idea. I'll add that to the 'tool kit' and play around with that as well.....this arcane-chicanery must be what causes some advocates to lean toward the 'managed code' camp...where we likely can't have this kind of 'fun' with the language :-D;) Just trying to keep the forces of entropy at bay

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #5

            Hey, exploring seamingly nutty ideas may indeed evolve into something really cool. Just look at ATL and WTL - now there are some wild and crazy ideas that have evolved into something cool and useful :) And the advocates can bite me and my gotos. ;) -- Please state the nature of your medical emergency.

            R 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              Hey, exploring seamingly nutty ideas may indeed evolve into something really cool. Just look at ATL and WTL - now there are some wild and crazy ideas that have evolved into something cool and useful :) And the advocates can bite me and my gotos. ;) -- Please state the nature of your medical emergency.

              R Offline
              R Offline
              RedZenBird
              wrote on last edited by
              #6

              Jörgen Sigvardsson wrote: And the advocates can bite me and my gotos I'm with you buddy! I've been known to sprinkle a goto on an as-needed basis (much to the chagrin of some 'religious types')....but only when it does not violate the idea of 'good taste':-D Just trying to keep the forces of entropy at bay

              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