Really strange compiler behaviour

Jan 5, 2015 at 9:34pm
I (accidentally) had code along the lines of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
class foo {};

class bar {
public:
   foo x;

   void doStuff ();
};

void bar::doStuff () {
   foo x = new foo ();
// Try to do something intelligent and fail miserably...
}


The question is: Why does this compile/what on earth is it doing??
Jan 5, 2015 at 9:50pm
The code should not compile. new foo () returns a pointer so x has to be a pointer.

 
foo* x = new foo ();

Jan 5, 2015 at 10:23pm
The x in doStuff is not related to the x class member variable - it shadows it.
Topic archived. No new replies allowed.