Your provided sample code is not actually using <tchar.h>
(your character type should be _TCHAR
in that case), but rather the basic C (not C++) string handling functions.
The idea of replacing this with std::string
is very much the right idea - and you may wish to throw out the tutorial you are using for attempting to teach you differently.
The code you attempted to create has a few problems though. First off, the _type
member field is now a std::string
that cannot be implicitly converted to a char*
, so your definition of the type
function will not compile. Since you are modifying the example anyway, you can just return a const
reference to your member:
class Product {
protected:
std::string _type;
Product() = default;
Product(Product const&) = default;
Product& operator=(Product const&) = default;
Product(Product &&) = default;
Product& operator=(Product &&) = default;
Product(std::string _type) : _type(std::move(_type)) { }
public:
std::string const& getType() { return _type; }
};
Note that I also removed the erroneous Coffee()
constructor (I assume that you intended to write Product
here).
By writing Product() = default;´, we instruct C++ to automatically generate an empty constructor. You may have noticed that this constructor is now
protected- we do not want the user of your class to be able to create a
Product` directly. (This may be the wrong thing to do, depending on your application.)
I have done the same thing to the copy and move operators to prevent object slicing, which many aspiring C++ programmers that first dip their toes into inheritance stumble over.
Finally, there is a constructor Product(std::string)
that will initialize the _type
member.
In the next step, you can derive from Product
:
class ConcreteProduct : public Product {
public:
ConcreteProduct() : Product("ConcreteProduct") { }
ConcreteProduct(ConcreteProduct const&) = default;
ConcreteProduct& operator=(ConcreteProduct const&) = default;
ConcreteProduct(ConcreteProduct &&) = default;
ConcreteProduct& operator=(ConcreteProduct &&) = default;
};
Note that we are now making the copy and move constructors public and are able to use the constructor syntax to intialize the base Product
directly.
Full example