Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 1.17 KB

compiler-warning-level-1-c4395.md

File metadata and controls

48 lines (39 loc) · 1.17 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Learn more about: Compiler Warning (level 1) C4395
Compiler Warning (level 1) C4395
11/04/2016
C4395
C4395
8051469a-3a39-4677-80f7-1300fbffe8ea

Compiler Warning (level 1) C4395

'function' : member function will be invoked on a copy of the initonly data member 'member'

A member function was called on an initonly (C++/CLI) data member. C4395 warns that the initonly data member cannot be modified by the function.

The following sample generates C4395:

// C4395.cpp
// compile with: /W1 /clr
public value class V {
public:
   V(int data) : m_data(data) {}

   void Mutate() {
      System::Console::WriteLine("Enter Mutate: m_data = {0}", m_data);
      m_data *= 2;
      System::Console::WriteLine("Leave Mutate: m_data = {0}", m_data);
   }

   int m_data;
};

public ref class R {
public:
   static void f() {
      System::Console::WriteLine("v.m_data = {0}", v.m_data);
      v.Mutate();   // C4395
      System::Console::WriteLine("v.m_data = {0}", v.m_data);
   }

private:
   initonly static V v = V(4);
};

int main() {
   R::f();
}