Sign in to follow this  
  • entries
    25
  • comments
    27
  • views
    13990

A warning would be nice

Sign in to follow this  

259 views

Grrrr....


class X { }

class M
{
public:
M(X* x) {}
}

class Y
{
private:
M m;
X* x;
public:
Y(X* nx):
x(nx),
m(x) {}
}



The above code raises an exception when attempting to create a new class Y. The order of attributes is M, X*. In the constructor, x is initialised with nx, then m is initialised with the class attribute x.. this is not the case. Initialisation order is done in order of specification in code,

so the constructor actually executes like so:


Y(X* nx):
m(x),
x(nx) {}



As specified in C++ standard.

VC++ 2003 does not give any warnings or errors during compilation about using attributes before they've been assigned a value, it'd be helpful if it did :(
Sign in to follow this  


2 Comments


Recommended Comments

Just to clarify, are you compiling at the highest warning level (/W4)? I know on level 4 it will complain about using variables without initializing them.

Share this comment


Link to comment
Thanks for that but unfortunately it still doesn't inform me that something will go horribly bad X(

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now