tags:

views:

38

answers:

1
//CCodeWrapperIF.h
public ref class CCodeWrapperIF
{
public:
  CCodeWrapperIF// Default constructor

public:

  static UINT8 funny;

  void foo(void);

}


//CCodeWrapperIF.cpp
extern "C"
{
 #include "CCodeWrapperIF.h"
}

[DllImport("CCode.DLL", CallingConvention = CallingConvention::Cdecl)]
extern "C" void CCode_Foo(void);

CCodeWrapperIF::CCodeWrapperIF(void)
{

}

CCodeWrapperIF::foo(void)
{
   CCode_Foo();
}



//a.h
public ref class A
{
private:  static CCodeWrapperIF^ CCode_IFObject;
A(void)
{
   CCode_IFObject=gcnew CCodeWrapperIF();
}

}

//b.h


public ref class B
{
   private:  static CCodeWrapperIF^ CCode_IFObject;

   B(void)
   {

   }
}


//main.h
int main(cli::array<System::String ^> ^args)
{
  A^  aObj=gcnew A();
  B^  bObj=gcnew B();

  // Funny thing is :  bObj->CCode_IFObject->funny has correct value always!
  // while if you watch the value of bObj->CCode_IFObject acturally it is not defined!! 
}

can anyone explain this?

A: 

Static members don't need an instance.

bObj->CCode_IFObject->funny is converted at compile-time to refer directly to CCodeWrapperIF::funny.

EDIT: Adding relevant text from the standard, section Class member access [expr.ref], wording from the C++0x FCD

If E2 is declared to have type “reference to T,” then E1.E2 is an lvalue; the type of E1.E2 is T. Otherwise, one of the following rules applies.

— If E2 is a static data member and the type of E2 is T, then E1.E2 is an lvalue; the expression designates the named member of the class. The type of E1.E2 is T.

— If E2 is a non-static data member ... the expression designates the named member of the object designated by the first expression.

...

As you can see, for non-static members the "object delegated by the first expression" must be valid. But static members can be identified by the dot notation just like non-static members, and the first expression does not have to be any object at all, only the class matters.

Ben Voigt
is the C++ standard described or permitted? Can this usage pass compilation under VC or GCC?
Biwier
It is expressly permitted by the standard. See edit.
Ben Voigt
my example is private: static CCodeWrapperIF^ CCode_IFObject; and every class can use the same value of CCode_IFObject->funny even the class doesn't new/gcnew it just define it is ok ! in short, The program only have one copy of funny becuase the CCode_IFObject only created once somewhere. for such situation, there are two ways to access it .#1 use CCodeWrapperIF::funny to get it #2 declare one object CCode_IFObject of CCodeWrapperIF class and no need to initialize it and then use CCode_IFObject->funny to get it, that's ok also .what you pasted explained the #1 but i am intereting is the #2.
Biwier
that's my understanding...if i am wrong please correct me thanks
Biwier
If you'll notice, nothing in the portion of the standard I quoted says *anything* about the scope resolution operator (`::`). The paragraph above what I quoted says that `->` is turned into `.`, for the purposes of our discussion everything about `E1.E2` applies to `E1->E2` as well.
Ben Voigt