Jul 20, 2013

[C++][NOTE][ORIGINAL] Dark Side of C++ - 1

Although compiler will prevent the incomplete type to be

initialized at class level; however, it is allowed to be

used in the function level. Since the function won't be

"initialized" until it's being used, which at that time,

the incomplete type should become complete. But, yes, but,

constructor itself is also a function. And it must be called

while the derived class object being initialized.

Ta da, the incomplete type will be forced to trigger and

an infinite loop has being created.

That's,


Segmentation fault


Welcome to the C++ world.


ideone:


template<typename T>
struct Base
{
   void ha(T t);
 
   Base(){
      T a;
   }
};
 
 
struct Derived : Base<Derived>
{
   int _a;
   Derived() : _a(10) {
   }
};
 
int main(){
 
   Derived derived;
 
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.