• entries
12
26
• views
17409

# Don't do this

178 views

Imagine you have a C++ class 'program', which takes a reference to a stack in its constructor.

templatestruct program{  typedef T value_type;  stack& stk;  program(stack& stk): stk(stk)  {}   void operator()(){}};

That's all very well, until you decide to have the stack as a member variable, and it changes to this.
templatestruct program{  typedef T value_type;  stack stk;  program(): stk(stk)  {}   void operator()(){}};

Obviously I should have got rid of the stk(stk) initialiser, but this is a really simpilified example; in my code the constructor code was in a different file and there were about 15 initialisers.

Your stk is now being initialised by itself. On my system, which was g++ 3.3.3 on cygwin, it went into an infinite loop where it eventually ran out of memory.

My question is, shouldn't it have at least warned me about that?

Hey there MrEvil! Welcome to journal land! I don't think I can answer your question, but compilers don't usually warn of run-time errors, those are usually out of it's scope.

And a hearty rating++ to you :)

Hey... Journal ++ over here as well. Welcome. :)

Superhappyratingsrape. Welcome to Journal Land. One for the programmer team, whoo hoo!

Do not use evil_fraction_math(). It will be your downfall.

-IV

Thanks [smile]

I guess this means I have to update it once in a while.. [grin]