CT 2024.12에서 클래스 코드를 활용하여 std::shared_ptr를 생성할 수 있습니다. 생성하는 순서는 다음과 같습니다.
- 클래스 코드로 포인터 객체 만들기
- 1.에서 만든 포인터 객체로 std::shared_ptr 객체 만들기
예제
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Animal {
public:
// Pure virtual function
virtual void speak() const = 0;
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void speak() const override {
std::cout << "Woof!" << std::endl;
}
};
class Cat : public Animal {
public:
void speak() const override {
std::cout << "Meow!" << std::endl;
}
};
class AnimalShelter {
public:
~AnimalShelter() {
}
void addAnimal(std::shared_ptr<Animal> animal) {
animal->speak();
animals.push_back(animal.get());
}
void makeAllSpeak() const {
for (size_t i = 0; i < animals.size(); ++i) {
animals[i]->speak();
}
}
void makeAnomyAnimal() {
std::shared_ptr<Animal> a = std::make_shared<Cat>();
a->speak();
}
private:
std::vector<Animal*> animals;
};
int main() {
AnimalShelter shelter;
shelter.makeAnomyAnimal();
return 0;
}
[사용자 코드]에서 사용하기
addAnimal
의 테스트 코드는 std::shared_ptr 객체 생성이 제대로 되지 않아 시그널 오류가 발생하고 있습니다.
[스텁]에서 사용하기
Need more help with this?
Don’t hesitate to contact us here.