Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Why can't PODs have constructors?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="BobR, post: 2987534"] PODs do have Ctor, copy-Ctor, and assignment operators. Otherwise you would not be able to use them in std containers (like std::vector). You just can't write your own and keep it a POD ('C'). (IMHO.) A little extra step can do that. This uses a 'RECT' from windows. It's a POD 'C' struct with 4 longs. Try this with your own POD struct. struct MyRect : public virtual RECT{ MyRect( long x1 = 0, long y1 = 0, long x2 = 10, long y2 = 10 ){ left = x1; // init the RECT members bottom = y1; right = x2; top = y2; } ~MyRect(){ this->RECT::~RECT();} RECT Rect(){ return *this;} }; { // main or ? using std::cout; // for NG post MyRect rect( 10, 12, 22, 42 ); cout<<" MyRect rect.bottom="<< rect.bottom << std::endl; RECT rect1 = rect.Rect(); cout<<" RECT rect1.bottom="<< rect1.bottom << std::endl; cout<<" sizeof(rect)="<<sizeof( rect )<<std::endl; cout<<" sizeof(rect1)="<<sizeof( rect1 )<<std::endl; } /* - output - MyRect rect.bottom=12 RECT rect1.bottom=12 sizeof(rect)=20 sizeof(rect1)=16 // sliced back to POD // (sixeof long == 4 on my sys) */ Of course it would take something a little more complex to justify the extra layer, or you would just do: RECT rect2 = { 14, 19, 25, 55}; [ corrections, comments welcome. ] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Why can't PODs have constructors?
Top