C++ Gurus,
I'm in the process of learning C++, and make every effort to use the Visual Studio 2010 debugger to aid in that process. Right now, I'm exploring static class data members.
I've run into an issue where the debugger is unable to find / display a 'static const' class data member. And am wondering if somebody here can explain why I can't add that entity to the debugger's "watch list". It's interesting, in that I
can get the debugger to dispaly 'static' (but not const) class data members.
Here's the simple code I'm using ...
DemoStaticClassMembers.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
//
// Toy class for learning about static class member initilization
// and accessing rules.
//
using namespace std;
class ToyClass {
public:
ToyClass(int a, int b) {
cout << "Running Constructor\n";
Object_Data_Member1 = a;
Object_Data_Member2 = b;
}
static const int Static_Class_Member1 = 1234; // initialize "in place"
static int Static_Class_Member2; // don't initialize
private:
int Object_Data_Member1;
int Object_Data_Member2;
};
|
... and DemoStaticClassMembers.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
//
// Simple program to demonstrate the usage of Static class data members, and
// to figure out why Visual Studio debugger can't display static const class data members.
//
#include <iostream>
#include "DemoStaticClassMembers.h"
using namespace std;
int ToyClass::Static_Class_Member2 = 94; // have to define value in file scope (not within a function)
int main(int argc, char* argv[]) {
cout << "Pre-Object Creation: \n";
cout << " Initialized static const 'Static_Class_Member1' = " << ToyClass::Static_Class_Member1 << "\n";
cout << " Uninitialized static 'Static_Class_Member2' = " << ToyClass::Static_Class_Member2 << "\n";
ToyClass Object1(1,2);
ToyClass Object2(3,4);
// ToyClass::Static_Class_Member1 = 16; // will compiler let me do this? No. Cannot modify a const.
// ToyClass::Static_Class_Member2 = 4; // will compiler let me do this? Yes. It compiles OK - but linker
// stage fails because this member hasn't been defined outside of a function
cout << "Post-Object Creation: \n";
cout << " Initialized static const 'Static_Class_Member1' = " << ToyClass::Static_Class_Member1 << "\n";
cout << " Uninitialized static 'Static_Class_Member2' = " << ToyClass::Static_Class_Member2 << "\n";
}
|
The Static_Class_Member1 class data member really exists, and it got initialized as expected (witness the program's output) ...
Pre-Object Creation:
Initialized static const 'Static_Class_Member1' = 1234
Uninitialized static 'Static_Class_Member2' = 94
Running Constructor
Running Constructor
Post-Object Creation:
Initialized static const 'Static_Class_Member1' = 1234
Uninitialized static 'Static_Class_Member2' = 94
|
Yet, if I try to add ToyClass::Static_Class_Member1 to the debugger's "watch list", I get an error message saying "CXX0070: Error: static member not present".
Is there some other notation I need to use to get the debugger to show me the contents of that 'static const' class data member?
Is this a "bug" in the debugger?
Thanks to all (in advance) who can enlighten me.