I have two classes, A and Bar, both share a header file that has essentially Foo* foo in it. Class A instantiates an objec开发者_如何学Pythont Bar* bar. This works fine. However, if I make the instantiation of the object
Bar* bar = new Bar();
I get an access violation when bar attempts to do something with foo. Why does this make a difference?
If I don't use 'new' it works fine. This is the error:
Unhandled exception at 0x003c17ea in Direct3DTutorial7.exe: 0xC0000005: Access violation reading
location 0x00000000.
Thanks.
0xC0000005: Access violation reading location 0x00000000.
This means you're dereferencing a null pointer, likely in the constructor of Bar
, or in some other code called by this constructor. Use a debugger to determine exactly where.
I would guess that you are not allocating your Foo object. As it is a global variable it is initialised to zero on program start-up, which for pointers corresponds to a null value.
Did you remember to construct a Foo object and assign it to the foo pointer? It sounds like your Bar constructor tries to do something with foo, but you haven't created the Foo object yet.
精彩评论