factory pattern의 스마트 포인터화 시켜봤는대 이게 맞는지 모르겠네요
201
강두리
작성한 질문수 17
0
//Example클래스는 변함이 없고
namespace jm
{
class Example : public Game2D
{
float time = 0.0f;
std::vector<std::shared_ptr <Shape>> my_objs;
public:
Example() : Game2D()
{
my_objs.push_back(Shape::makeTriangle(Colors::gold, vec2{ -0.5f,0.2f }, 0.25f));
my_objs.push_back(Shape::makeCircle(Colors::olive, vec2{ -0.1f,0.1f }, 0.2f));
my_objs.push_back(Shape::makeBox(Colors::black, vec2{ 0.0f,0.5f }, 0.1f, 0.3f));
my_objs.push_back(Shape::makeStar(Colors::gold, vec2{ 0.0f,-0.5f }, 0.25f,0.15f));
//my_objs.push_back(new Triangle(Colors::gold, { -0.5f,0.2f }, 0.25f));
//my_objs.push_back(new Circle(Colors::olive, { -0.1f,0.1f }, 0.2f));
//my_objs.push_back(new Box(Colors::black, { 0.0f,0.5f }, 0.1f, 0.3f));
}
~Example() {
}
void update() override
{
for (const auto &obj : my_objs) {
obj->draw();
}
}
};
//shape.h의 static 함수들을
shared_ptr화 시켜주는 작업
#pragma once
#include "Game2D.h"
#include "Shape.h"
namespace jm {
class Shape {
public:
vec2 pos;
RGB color;
void init(const RGB& _color, const vec2& _pos,float _r=0.0f)
{
this->color = _color;
this->pos = _pos;
}
virtual ~Shape() {
}
virtual void drawShape() const = 0; //pure virtual classes
void draw() {
beginTransformation();
{
translate(this->pos);
drawShape();
}
endTransformation();
}
static std::shared_ptr<Shape> makeTriangle(const RGB& _color, const vec2& _pos, const float& _size);
static std::shared_ptr <Shape> makeCircle(const RGB& _color, const vec2& _pos, const float& _size);
static std::shared_ptr<Shape> makeBox(const RGB& _color, const vec2& _pos, const float& _width, const float& _height);
static std::shared_ptr<Shape> makeStar(const RGB& _color, const vec2& _pos, const float& _outer_radius, const float& _inner_radius);
};
}
//이렇게 해준 후
//Shape.cpp
#include "Shape.h"
#include"Triangle.h"
#include"Circle.h"
#include"Box.h"
#include"Star.h"
namespace jm {
std::shared_ptr<Shape> Shape::makeTriangle(const RGB& _color, const vec2& _pos, const float& _size) {
return std::make_shared<Triangle>(_color, _pos, _size);
}
std::shared_ptr <Shape> Shape::makeCircle(const RGB& _color, const vec2& _pos, const float& _size) {
return std::make_shared<Circle>(_color, _pos, _size);
}
std::shared_ptr <Shape> Shape::makeBox(const RGB& _color, const vec2& _pos, const float& _width, const float& _height) {
return std::make_shared <Box>(_color, _pos, _width, _height);
}
std::shared_ptr <Shape> Shape::makeStar(const RGB& _color, const vec2& _pos, const float& _outer_radius, const float& _inner_radius) {
return std::make_shared <Star>(_color, _pos, _outer_radius, _inner_radius);
}
}
//위에처럼 바꿔주면 돼는거 같은대
//소멸자가 정상적으로 호출되는거 같거든요 cout 찍어보니까
//맞나요?
답변 1
깃허브에서 받은 코드가 왜 강의코드랑 다를까요
0
72
1
Mac vscode 으로 시작하시려는 분들께
0
313
1
마우스 좌우 버튼을 동시에 눌렀을 때의 원의 위치
0
424
1
정답은 어디서볼수있나요?
1
346
2
예제코드
1
490
2
따배C++ 몇 강까지 학습한 후 수강가능 할까요?
1
642
1
15:00 부근 질문 있습니다.
0
419
1
(20.4 참고) 20.4에 해당하는 가이드 페이지가 어디있는지 모르겠습니다.
0
371
1
multimap 질문
0
353
2
도형들의 움직임이 너무 빠릅니다...
0
463
1
vcpkg 설치를 했는데
0
682
2
mutiple bullet 관련 질문 드립니다.
0
480
2
실행 후 화면 꺼짐
0
578
2
예제 파일 실행 시, 에러
1
558
1
아직 못풀어도 괜찮을까요?
0
499
1
코드 열었을 때 오류
1
807
3
코딩공부에 대해서 막히는부분
0
334
1
vcpkg 설치 오류
0
592
2
랜덤값 질문입니다.
0
425
1
vcpkg 다운로드에 문제를 겪고있습니다
1
423
1
if 문에 >= 대신 == 넣으면 작동을 하지 않는 이유가 무엇인가요.
0
281
1
multiple bullet 문제
0
314
1
프로그램 실행 순서 질문
0
270
1
2.2.2 상속으로 깔끔하게 init 메서드 질문
0
225
1





