I've been using a pattern in a library I'm creating that uses passes a String name of an object to its base object's constructor. I've tried using std::string and c-style strings but keep getting weird memory errors with Valgrind.
class Base {
public:
Base( std::string name ) : name(name) {}
virtual ~Base() {}
std::string getName() { return name; }
private:
std::string name;
};
class Derived : public Base {
public:
Derived() : Base("Derived") {}
};
int main() {
Base* derived = new Derived;
std::cout << derived->getName() << "\n";
delete derived;
}
(This compiles and runs fine in Valgrind)
Is something like this safe? I'm using 'const char*' instead of 'std::string' right now, is that safe?
Is there a safer alternative, preferably without using virtuals?
Edit: Is there a way to do this with templates? I don't want to use RTTI since it has the name mangled and I want the name to be 'normal' for use with scripting/data persistance.
Everything you do here is fine.
Templates would get you nothing because you still need to store a runtime pointer in the base class for dynamic identification.
Smart pointers would get you nothing because the lifetime of the string is the entire program. If you aren't computing anything, char const * and initialization from a string literal are ideal. If you are computing the string, then you can use static std::string const wrapped in a getter function.
class Derived : public Base {
public:
Derived() : Base(get_name()) {}
private:
static std::string const & get_name() {
static std::string const name = "Derived"; // or = compute_name();
return name;
}
};
This avoids the static initialization order fiasco. (The getter function receives an extra multithreading-safe guard from the compiler.) The lifetime of the string is the lifetime of the program. The Base may store a string const & or a char const *, it doesn't really matter. I would recommend char const * because the string reference could potentially be accidentally initialized with a temporary.
Related
I'm new to OOP and I'm working on a C++ project. I isolated my problem to make answering easy but here's the real scenario:
I have a superclass member function, that modifies values inside the object that called it. The modification is based on a value coming from another object of the same class. This object is given to the function as the only parameter. Such as:
void BaseClass::function(BaseClass x) {}
However, I created a subclass. And if the parameter is a subclass type, I want to modify its unique attribute, too.
void BaseClass::function(DerivedClass x) {}
The problem is that the subclass is obviously defined later in the code.
I don't want it as two separate methods, because the calculation algorithm is already written inside, and also the solution I search for doesn't require to change the code at the places where the function is already in use. Besides, every other possibility that comes to mind (e.g. using typeid()) looks silly.
#include <iostream>
#include <string>
class Base
{
protected:
//common attribute
const std::string name;
public:
//constructor for common attribute
Base(const std::string nameString) : name(nameString) {}
//getter
std::string getName() { return name; }
//superclass as parameter
void test1(Base &example) { std::cout << example.getName(); }
//subclass as parameter (I'd want the line below to work)
//void test2(Derived &example) { std::cout << example.getNumber(); }
};
class Derived : private Base
{
protected:
//unique attribute
const std::string number;
public:
//constructor
Derived(const std::string nameString, const std::string numberString) : Base(nameString),
number(numberString) {}
//getter for unique attribute
std::string getNumber() { return number; }
};
int main ()
{
Base object = Base("whatever");
Base baseParameter = Base("base");
Derived derivedParameter = Derived("derived", "12");
object.test1(baseParameter);
//object.test2(derivedParameter);
return 0;
}
What is the standard way of doing it?
You could make test2 a template, and ensure that it's only used with types derived from Base:
template<typename Derived>
void test2(Derived &example)
{
static_assert(std::is_base_of_v<Base, Derived>);
std::cout << example.getNumber();
}
Here's a demo.
I know this might look like a trivial question, but I haven't found really an elegant C++ solution to the following problem.
I want to represent a complex (tree-like) hierarchy of a "world" of objects. Let's say Animals. Every animal has some basic const properties.
Like for example a name. Then it also has some methods, but they are not significant for this problem.
class Animal {
public:
const char *GetName() const;
protected:
const char *name;
};
class Insect : public Animal {
...
};
class Butterfly : public Insect {
...
};
In this hierarchy I would like to initialize the name in every derived (grand)child. What is an elegant solution to this?
It is also important to say that in this "world" there be only instances of the tree leaves. That is, there will be no objects "Animal" or "Insect". But there will be objects "Butterfly", "Bee" or "Mosquito".
I know the "standard" way to do this is to put name into constructor:
Animal::Animal(const char *name) : name(name) {}
Insect::Insect(const char *name) : Animal(name) {}
Butterfly::Butterfly() : Insect("Butterfly") {}
But if there are more of these properties, the derived classes need also some initialization and the hierarchy has more levels it can become quite a mess:
Animal::Animal(const char *name) : name(name) {}
Vertebrate::Vertebrate(const char *name) : Animal(name) {}
Mammals::Mammals(const char *name) : Vertebrate(name) {}
Ungulate::Ungulate(const char *name) : Mammals(name) {}
Horse::Horse() : Ungulate("Horse") {}
Another option I can see is to drop the const and assign directly in the grandchild's constructor:
class Animal {
public:
const char *GetName() const;
protected:
std::string name;
};
Horse::Horse() {this->name = "Horse";}
But that is also not optimal, because the const is lost and it is more prone to errors (the initialization can be forgotten).
Is there some better way to do this?
Hm - hope that I get not locked out from SO for that answer, but you could use a virtual base class that implements the name-property. Thereby, you will not have to propagate initialization in a base class all way through the hierarchy but could directly address the "very base" constructor with the name-property. Furthermore, you will actually be enforced to call it in any "Grandchild"-class, so you can't forget it by accident:
class NamedItem {
public:
NamedItem(const char* _name) : name(_name) {}
const char *GetName() const;
protected:
const char *name;
};
class Animal : public virtual NamedItem {
public:
Animal(int mySpecificOne) : NamedItem("") {}
};
class Insect : public Animal {
public:
Insect(int mySpecificOne) : Animal(mySpecificOne), NamedItem("") {}
};
class Butterfly : public Insect {
};
The elegant solution is to pass arguments through initialisation. For example, if the "name" variable was the name of the Butterfly (such as "sally" or "david") then it would be obvious it has to be done through initialisation. If you are finding that is ugly, as it is here, it may indicate that your data decomposition/class heirarchy are at fault. In your example every Butterfly object would have an identical set of properties that really refer to their class rather than each instance, ie they are class variables not instance variables. This implies that the "Butterfly" class should have a static pointer to a common "Insect_Impl" object (which might have a pointer to a single "Animal_Impl" object etc) or a set of overridden virtual functions. (Below I only show one level of heirarchy but you should be able to work out more levels)
// Make virtual inherited functionality pure virtual
class Animal {
private:
std::string objName; // Per object instance data
public:
virtual ~Animal(std::string n): objName(n) {}
virtual std::string const& getName() = 0; // Per sub-class data access
virtual std::string const& getOrder() = 0; // Per sub-class data access
std::string const& getObjName() { return this->objName; }
};
// Put common data into a non-inherited class
class Animal_Impl{
private:
std::string name;
public:
Animal_Impl(std::string n): name(n);
std::string const& getName() const { return this->name; }
};
// Inherit for per-instance functionality, containment for per-class data.
class Butterfly: public Animal{
private:
static std::unique< Animal_Impl > insect; // sub-class data
public:
Butterfly(std::string n): Animal(n) {}
virtual ~Butterfly() {}
virtual std::string const& getName() override {
return this->insect->getName(); }
virtual std::string const& getOrder() override {
static std::string order( "Lepidoptera" );
return order; }
};
// Class specific data is now initialised once in an implementation file.
std::unique< Animal_Impl > Butterfly::insect( new Animal_Impl("Butterfly") );
Now using the Butterfly class only needs per-instance data.
Butterfly b( "sally" );
std::cout << b.getName() << " (Order " << b.getOrder()
<< ") is called " << b.getObjName() << "\n";
The issue with your alternative, or any alternative leaving name non-const and protected, is that there is no guarantee that this property is going to be setup properly by the subclasses.
What does the following class give you ?
class Animal {
public:
Animal(const char* something)
const char *GetName() const;
private:
const char *name;
};
The guarantee of the immutability of the Animal interface, which can be a big plus when doing multithreading. If an object is immutable, multiple threads can use it without being a critical resource.
I know the "standard" way to do this is to put name into constructor:
... But if there are more of these properties, the derived classes
need also some initialisation and the hierarchy has more levels it can
become quite a mess
It is not messy at all. Given that there is only one place where the members of object A are being initialised, and it is within the constructor of their subclasses.
I have a program with a main function that simply prints a string. When I run this program it crashed without output in the console. I found out the problem happens when I insert an element into the map of OperatorCore (symbolMap).
This is the minimal code:
//Binary.hpp
class Binary final : public OperatorCore, public StaticPool<Binary> {
public:
Binary(int ID, std::string name)
: OperatorCore(name), StaticPool<Binary>(ID) {
}
~Binary() {}
};
//Binary.cpp
template<>
const Binary StaticPool<Binary>::pool[] = {
Binary(0, "a string value")//without this line of code, it prints works
};
//OperatorCore.hpp
class OperatorCore {
public:
static std::map<std::string, OperatorCore*> symbolMap;
const std::string name;
OperatorCore (std::string name);
virtual ~OperatorCore () {}
};
//OperatorCore.cpp
std::map<std::string, OperatorCore*> OperatorCore::symbolMap{};
OperatorCore::OperatorCore(std::string name) : name(name) {
symbolMap.insert({name, this});
}
//StaticPool
template<typename T, typename TKey = int>
class StaticPool {
public:
const TKey ID;
static const T pool[];
StaticPool(TKey ID) : ID(ID) {}
virtual ~StaticPool() {}
};
The problem does not occur if I delete one of the highlighted lines. Does this design cause a memory corruption?
EDIT: The initialization of OperatorCore::symbolMap is in the same file where is also the implementation of OperatorCore constructor.
I suspect you have an order-of-initialization problem. In other words, your program tries to initialize pool before symbolMap, and the constructor of the Binary object calls the base constructor, which tries to use symbolMap, which hasn't been constructed yet.
There are a number of solutions for this. Your best bet is probably to turn the symbolMap static member into a static variable inside a special getter function:
class OperatorCore {
public:
static std::map<std::string, OperatorCore*>& symbolMap() {
static std::map<std::string, OperatorCore*> instance;
return instance;
}
};
You have a case of static initialization order fiasco. Meaning that your statics might be constructed in the wrong order. I do not know if initializing the static objects in the same file in the correct order works. But otherwise you should delay the initialization of the pool which depends on the map. Maybe write a function to initialize the pool and call it at the start of your main.
I'm having problems getting a vector of objects to print the name of the derived class rather then the base class.
These are my classes
#include <vector>
#include <iostream>
using namespace std;
class Object
{
string name;
public:
string getName()
{
return name;
}
};
class Flashlight : public Object
{
string name = "Flashlight";
public:
string getName();
};
This is my main, it has a vector of the objects and at this point just needs to print out their names.
int main()
{
vector<Object> items;
items.push_back(*new Flashlight);
for(int i = 0; i < items.size(); i++)
{
cout << i+1 << " " << items.at(i).getName();
}
}
Right now if I assign something to the name in Object it will print that but its not using the derived classes values, I just want it to inherit the function then use its own values with it. I've tried implementing the function in the base classes(but seeing as I could have a lot of those in future it would lead to lots of redundant code) but that doesn't work either.
Instead of declaring variable 'name' in derived classes, initialize the variables value as custom value for that derived class. Also I would suggest you to make getName method as virtual. You also do not need to override getName method in derived classes if you just want to output name.
Try this way:
int main()
{
vector<Object *> items;
items.push_back(new Flashlight);
for(int i = 0; i < items.size(); i++)
{
cout << i+1 << " " << items.at(i)->getName();
}
}
Also as mentioned in another answer it would be good to use virtual methods read about it here.
What is more you need to use constructor to set value for variable name. You can read about them here
You should try this way:
class Object
{
private:
string name;
public:
Object() : name("Object")
{}
Object(string str) : name(str)
{}
string getName()
{
return name;
}
};
class Flashlight : public Object
{
public:
Flashlight() : Object("Flashlight")
{}
string getName();
};
Your code has two problems:
getName() is not virtual. This means that the actual function called is decided at compile time, based on the variable static type. So, if your variable is of type Object, Object& or Object* (or any const variation on those), function Object::getName() will be called, regardless of the actual type of the object referred to or pointed to by your variable (the dynamic type).
You have a vector<Object>, which means that all objects stored in it are of type Object. You can try to put a Flashlight object in it, but only the Object part will be copied into the vector. If you need polymorphism in a vector<>, you need a vector of pointers (or even better, smart pointers).
So you have to do two things to get the desired behaviour:
Declare function getName() as virtual string getName() (even
better, virtual const string& getName() const)
Declare items as vector<Object*> items (even better, use an
appropriate smart pointer such as unique_ptr<> instead of a raw
pointer)
Ok, this example is pretty straight-forward for the concept I'm trying to understand. I'll just show you the code:
class Base
{
protected:
string name;
public:
virtual string getName() const { return this->name; }
virtual void setName(string name) { this->name = name; }
....
}
class Derived : public Base
{
private:
double price;
....
}
main(int argc, char** argv)
{
Base* base = new Base("Base Class");
Derived* derived = new Derived(base, 453.21);
derived->setName("Name changed!");
cout << "Name of instance: " << base->getName() << endl;
// Desired effect
Output: 'Name changed!'
// Actual effect
Output: 'Base Class'
....
}
The issue for me is this. I want to create an instance of derived class with reference to already created instance of base class, so when I change any member variable of base class trough the derived instance, I can see the change on previously created base instance in the way demonstrated above.
Note: I hope that you will manage to comprehend what I meant, as I am aware that my terminology is probably little off. Please, don't be harsh. :)
Note: I won't be showing / writing constructors, since I am not sure what is the best way to do this, if even any exists and the syntax may be incorrect.
This seems to indicate the problem:
Base* base = new Base("Base Class");
Derived* derived = new Derived(base, 453.21);
as it is usually not necessary to construct the Base class separatly. Your derived class will already contain a Base instance implicitly, you don't have to add a pointer to one manually and set it from the ctor. I don't know how your ctor looks, but it should look like this:
Derived(const std::string& name, double p) : Base(name), price( p ) {}
If this enough to fix it yourself, good, otherwise post all the code of your example. Instead of the two lines from your code that I quoted above, it should look more like:
Derived* derived = new Derived("Base Class", 453.21);
If you post the code of Derived, it should be obvious for us and it will be much easier to explain it to you on your concrete example.
The way you are trying to do is weird, but you can simply use inheritance concept like this code:
class Base
{
public:
Base(const string &name) : name(name) {}
virtual void setName(const string &name) { this->name = name; }
virtual string getName() const { return name; }
protected:
string name;
};
class Derived : public Base
{
public:
Derived(const string &name, double price) : Base(name), price(price) {}
private:
double price;
};
int main()
{
Derived* derived = new Derived("Base Class", 453.21);
derived->setName("Name changed!");
Base *base = derived;
cout << "Name of instance: " << base->getName() << endl;
}
Output
Name of instance: Name changed!
You don't need a create Base object and pass it to the derived object.
Instead, create a derived object and pass its address to a Base pointer.