std :: make_shared字符串参数

时间:2016-01-10 13:13:33

标签: c++ c++11 makefile shared

我偶然发现了一个奇怪的怪异

animal.h

#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
#include <iostream>
#include <vector>
#include <memory>
class Animal
  {
  public:
    Animal(std::string aType);
    void someFunction();
  private:
    std::string type;
  };

#endif // ANIMAL_H

animal.cpp

Animal::Animal(std::string aType) :
  type{aType}
  {
  }
void Animal::someFunction(){
/*Code*/
  }

我在“Animal”类上调用std :: make_shared,参数为std::string

的main.cpp

auto sheep = std::make_shared<Animal>("Sheep");

现在我在animal.cpp类中也有函数。

如果我这样定义sheep,我就无法调用任何函数。 (sheep->没有做任何事情。) 我明白了:

no matching function for call to 'Animal::someFunction()'

但是,如果我改变:

auto sheep = std::make_shared<Animal>("Sheep Blabla");

我可以在羊身上调用一个函数。

sheep->someFunction();

给了我Animal类中定义的所有函数。 只需在羊背后加上“Blabla”这个词。

这怎么可能?

0 个答案:

没有答案
相关问题