2015. 6. 22. 15:00



-------------------------------------------------------------------------------------


c++은 모두 value type


c#은 type이 지정되어있다


c++은 사용자가 필요에 따라 포인터라는 기능을 사용해 메모리 위치를 정할수있지만


c#은 닷넷에서 알아서 분배해서 reference type은 아예 할당안하면 에러나고 그럼


즉 c++ 에서는 


class AAA

{}


void main()

{

AAA aaa;

aaa.test();


AAA aaa = new AAA();

aaa->test();

}

이런식으로 정적할당할지 동적할당할지 정할수 있었다. 모든것이 value type이기 때문


하지만 c#에서는 애초에 reference type인것은 무조건 동적할당을 해줘야 사용가능하다는 말임



-------------------------------------------------------------------------------------

issue about 'new ' keyword


[원문]http://stackoverflow.com/questions/23034541/operator-new-in-c-sharp-vs-c


변수, 객체 할당에 대한 내용

간단히 번역만 해둠


3가지 경우가 있다 1. 지역변수 

 2. (non-static)구조체 멤버변수

 3. 클래스 멤버변수


1.지역변수

class C

{

  void M()

  {

    Foo fooInstance = new Foo();

    //definite assignment - 접근해서 사용가능

    // the variable is "definitely assigned" and can be read (copied, passed etc)

    // consider using the 'var' keyword above!

  }

}


class C

{

  void M()

  {

    Foo fooInstance;

    // 이건 안됨. 접근 불가.  할당후에 사용가능함

    // the variable is not "definitely assigned", you cannot acquire its value

    // it needs to be assigned later (or can be used as 'out' parameter)

  }

}



2.(non-static)구조체 멤버변수

struct S

{

  Foo fooInstance = new Foo(); // compile-time error! cannot initialize here

       // 선언시 할당 불가

}


struct S

{

  Foo fooInstance; // OK, access level is 'private' when nothing is specified

                      // 특별히 접근제한자 안걸면 private으로 사용가능

}



3. 클래스 멤버변수

class C

{

  Foo fooInstance = new Foo(); // OK, private

}


class C

{

  Foo fooInstance; // OK, private

  // equivalent to 'Foo fooInstance = null;' if 'Foo' is a reference type (class, interface, delegate, array)

  // equivalent to 'Foo fooInstance = new Foo();' is 'Foo' is a value type (struct, enum) <- 이부분이 궁금함

아아 value 타입인경우 그냥 쓰나 이니셜라이저 사용하나 똑같다는 말임


 애초에 값이 reference타입인지 value타입인지 정해져있으면 new 키워드를 붙여도 무조건 그에 따라 스택 힙 결정되는건가

}


'C#/Unity' 카테고리의 다른 글

2D OthographicSize  (0) 2016.04.10
NGUI Drawcall  (0) 2015.09.15
Action, Func 콜백 예시  (0) 2015.06.12
c# where  (0) 2015.05.29
GetComponentsInChildren overload  (0) 2015.05.28
Posted by 반얀