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