포인터는 주소랑 같다.
구조체는 포인터의 형태가 다르다.
struct_pointer 예제1)
.
.
.
.
.
.
struct Text
{
float X;
float Y;
BYTE R;
BYTE G;
BYTE B;
string Content;
};
void Render()
{
Text text = { 100, 100, 255, 0, 0, "Test"};
RenderImGuiText(text.X, text.Y, text.R, text.G, text.B, text.Content);
text.Y = 200;
RenderImGuiText(text.X, text.Y, text.R, text.G, text.B, text.Content);
Text* p= &text;
p->Y = 300; //첫번째 Text 안에 Y를 바꾼것
RenderImGuiText(text.X, text.Y, text.R, text.G, text.B, text.Content);
}
<Struct_Pointer/main.cpp> = 헤더파일 변경으로 좀 더 축약됨
#include "stdafx.h"
#include "System/Device.h"
#include "Text.h"
//#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")
void InitScene()
{
}
void DestroyScene()
{
}
Text renderText;
void Update()
{
renderText.Content = "Player";
renderText.X += 0.1f;
}
void Render()
{
//RenderText(&renderText);
RenderText(renderText);
}
포인터는 가리키는 번지가 없으면 프로그램이 그냥 꺼짐
보통 포인터 보다 레퍼런스를 사용함
관례상 : 뭔가 값을 바꿔서 원본에 영향을 미칠려면 포인터 사용하고
뭔가 값을 받아서 쓰는 것이면 레퍼런스를 사용한다.
큰 덩어리를 할당 삭제 할당 삭제 하는 것은 좋은 생각이 아님 그러니 메모리 단편화를 최대한 피해주는 것이 좋다.
그 복사를 방지 하기 위해 레퍼런스를 사용한다.(이름만 붙여주는 것)
단지 이름만 붙여 주기에 공간이 생기는 것이 아니라. a라는 공간안에 b라는 이름을 붙여주는 것
Cpp에서는 함수의 이름으로 판단하는게 아니라 뒤에 나오는 파라미터의 계수와 타입으로 판단함
같은
오버로딩(Overloading) : 같은 이름의 메서드 여러개를 가지면서 매개변수의 유형과 개수가 다르도록 하는 기술
오버라이딩(Overriding) : 상위 클래스가 가지고 있는 메서드를 하위 클래스가 재정의해서 사용
<Struct_Pointer/Text.cpp> with Struct_Array
#include "stdafx.h"
#include "System/Device.h"
#include "Text.h"
void RenderText(Text* InText) //포인터를 빼면 복사가 일어남
{
RenderImGuiText(InText->X, InText->Y, InText->R, InText->G, InText->B, InText->Content);
}
void RenderText(const Text& InText) //자기공간에 이름만 붙은거니까. 직접접근참조로 바꾼다"."
{
RenderImGuiText(InText.X, InText.Y, InText.R, InText.G, InText.B, InText.Content);
}
(헤더파일 참고)- stdafx.h 파일에 따로 더 설정함(해당파일은 보안때문에 설정 못함)
#pragma once
void RenderText(Text* InText);
void RenderText(const Text& InText);
<Struct_Array/main.cpp >(배열)
#include "stdafx.h"
#include "System/Device.h"
#include "Text.h"
//#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")
void InitScene()
{
}
void DestroyScene()
{
}
Text renderText[3];
void Update()
{
for (int i = 0; i < 3; i++) //i 가 int 형 아래
{
renderText[i].Y = (float)(i * 50);
// i가 int 형이라서 손실 되는 것도 float을 변환하게 된다. float으로 들어갈때 손실된값 손실된걸 알고서도 쓰고 싶다면 저렇게 바꾸면 됨
//명시적 현변환
renderText[i].Content = "Game";
}
renderText[0].Y = 300;
Text* p = renderText;
//(p + 1) -> b = 20 //접근할려면 이렇게 해야함 1번 객체의 b에 접근하게됨
//*p //이러면 해당번호의 객체 전체를 의미하게됨 그 상태에서는 직접/간접접근 허용불가 포인터가 붙으면
}
void Render()
{
for(int i = 0; i < 2; i++)// 3 => 2 변경 만약 객체 전체를 사용해야 한다하면 //실행하면 가운데는0~2까지 줄여서 안나옴
RenderText(renderText[i]);
//전체참조로 변환
RenderText(*(renderText + 2));
//RenderText(renderText[2]); //바로위에 거랑 같음 //2번이 잘 등장하는 것을 볼 수 있다.
}
https://boycoding.tistory.com/177 - 소년코딩 암시적 명시적 형변환
'Programming > DirectX 2D' 카테고리의 다른 글
[C++ ] DirectX 점과 선과 사각형의 원리/ #define /VSPS (2) | 2021.11.09 |
---|---|
[C++] DirectX Graphics part/ 2차 포인터 / 동적할당 (0) | 2021.11.08 |
[C++] DirectX Struct와 Class and Constructor 구조체 와 클래스 그리고 생성자 (0) | 2021.11.05 |