Accessing const object during constructor without this pointer

Pankaj Gupta pankaj@tower-research.com
Mon Dec 5 20:14:00 GMT 2005


Hi

I have a question. Consider this code:

#include <iostream>

void global_init();

class A {
public:
   int i;
   A() : i(10) {
     global_init();
   }
};

const A obj;

void global_init() {
   std::cout << "obj.i = " << obj.i << std::endl;
}

int main() {
   return EXIT_SUCCESS;
}



Here, global_init() is accessing a subobject of a const object, while its 
being constructed. I think the standard says that, if the access in not 
being made through the constructor's "this" (directly or indirectly), the 
value of the const object or any subobject is unspecified.

But when I compile using g++, I don't get any warnings about it.

Any idea if this should be giving warnings or not.


Best Regards
Pankaj



-- 

Pankaj Gupta

Infrastructure Team
Tower Research Capital

pankaj.gupta@tower-research.com  [Work]
pankaj.gupta@yahoo.com           [Personal]





More information about the Gcc mailing list