@
Cambalinho
Hi,
In addition to the existing answers:
This is classic XY problem:
Cambalinho wrote: |
---|
if i use class object and class member, i must test if returns a value or if it's a member ;) |
You have come up with a possible solution Y, but we don't know what the problem X is. We might have a better solution.
Please provide a detailed example of what you are doing exactly. :+)
Maybe this can be solved with virtual polymorphism, or maybe it requires some Template Meta Programming (TMP), a design pattern (DP), Policy Design, a mixture of those, or something else : we can't say.
As an example, say we have various Transport Mechanisms: walking, riding a bicycle, moped (electric or petrol), motorbike, car (petrol, diesel, electric, LPG, solar), ride an animal (donkey, horse, camel). We want to know what resources are needed to use a Transport Mechanism, noting animals including humans need food and water. We want to add new things without having to change the code too much. Using virtual polymorphism, it could look like this:
1 2 3 4 5 6 7 8 9 10 11 12
|
class TransportMechanism;
class Vehicle : public TransportMechanism;
class Animal : public TransportMechanism;
class Resource;
class Fuel : public Resource;
class ElectricCharging : public Resource;
class SolarEnergy : public Resource;
class Food : public Resource;
class Water : public Resource;
|
There could be more derived classes, or use a Factory DP to make them. The resources may not be limited to things like fuel, one could have Sat Navs and other equipment, licensed drivers, approval to use etc.
The base classes would have pure virtual functions that are common, this is the interface.
Provide containers and functions that are written in terms of the base classes. For example a concrete object like
MyVeyron
could inherit from the
TransportMechanism
base class a:
std::vector<std::unique_ptr<Resource>> RequiredResources
This vector would have
concrete resources like petrol or food say
push_back
or
emplace_back
into it, in the derived class object. Polymorphism means that the derived class can then execute the correct function.
The
Resource
base class could have a pure virtual function or ostream operator that prints out what a particular resource is. The concrete resource class would have a member variable (also inherited from the Resource base class) whose value contains the actual data to be output by the ostream operator.
Naming of classes and deciding on what classes are inherited is important. For example, say we want to make the distinction between a bobsled, a horse drawn carriage and the other vehicles. We can make new classes and name them properly to accommodate these requirements, sometimes this means making new intermediate classes or even creating new base classes.
Going back to the car with engine example, we might have two-stroke and four stroke engines: Two Stroke has fuel with oil already in it; Four Stroke has fuel and separate oil. It is possible to implement this with virtual polymorphism.
Some other more advanced ideas:
https://en.cppreference.com/w/cpp/types#Type_traits_.28since_C.2B.2B11.29
https://en.cppreference.com/w/cpp/concepts
https://en.cppreference.com/w/cpp/experimental/constraints
There are plenty of members here who know all about polymorphism, if that is a solution to this problem, but first we need to know what the problem is. :+)
Good Luck !!