c++classobjectinstantiation

Single line class declaration and instantiation


I've stumbled upon code using the following syntax.

int main(){    
  class foo{
    public:
      int x;
      foo(int y){x=y;}
  }
  * bar = new foo(1);    
}

Is there any purpose/consequence of using it compared to the more common

int main(){    
  class foo{
    public:
      int x;
      foo(int y){x=y;}
  };

  foo * bar = new foo(1);    
}

Solution

  • This is probably a matter of opinion but I think the first method is poor coding practice.

    1. It does not impact run time behavior.
    2. It makes the code harder to read.