ID data member in c++ class - c++

My teacher required us to create ID data member that's generated automatically, and once established it can’t be modified.
What is the most appropriate type?
if the answer is
static const int ID;
How can I generate it automatically while it's const?

Since the ID has to be unique, one shall make sure, that two instances never get the same ID. Also, noone outside class should interfere in generating the UID.
First, you define a static field in your class:
class Data
{
private:
static int newUID;
(...)
};
// The following shall be put in a .cpp file
int Data::newUID = 0;
Then, after each instance is created, it should take a new ID value and increment the newUID counter:
class Data
{
(...)
const int uid;
public:
Data()
: uid(newUID++)
{
}
int GetUid()
{
return uid;
}
};
Noone has access to internal newUID except the class, ID is generated automatically for each instance and you are (almost1) sure, that no two instances will have the same ID number.
1 Unless you generate a little more than 4 billion instances

Here is an example:
class SomeClass {
static int currID;
public:
const int ID;
SomeClass() :
ID(currID++) { // Initialization list used to initialize ID
}
};
And you must put this somewhere in your .cpp file:
int SomeClass::currId = 0;
This uses initialization lists to do the trick. We must do it this way for it is only in initialization lists where you can assign to a const member in an instance.
The core concept is that you must have a "global setting" to keep track of the ID that would be assigned to instances created in the future. It is currID in this example. For each instance created in your class, you assign the value of currID as the ID of that instance then increment it (currID). In this way, you get instances which have unique IDs.

create a const ID as int , initialize it in constructor initialization list like
SomeClass(): id(++staticIncrementingInt){
///other stuf here
}
Hope this helps.

You can actually call a function (rand, in this case) in the construction initialization list.
Up to you now how you can seed it.

If your teacher allows boost libs then try Boost.Uuid. An example is here. It is RFC 4122 compliant.

Related

What is the best way to initialize a more complex class construct with many unchangeable members in c++

I'm currently designing classes that should represent a chaotic storage system.
Lets say we have slots in rows and columns with certain properties.
So the slots have different restrictions in min/max height, width, length, weight and some more that come from a parameter file.
Also the Slots have a max total weight that must be checked before a new parcel gets added to that slot. And also the max weight that a row of slots can hold is lower than the sum of the max weights of the single slots. So for example each individual slot might be able to hold 50kg but the row of 10 slots must not exceed 200kg, so it is not allowed to fill every slot by 100%. The same is true for the Columns where the maximum weight is lower than the sum of the individual weights of the single rows. The row_id and column_id are atrificial numbers for adressing the slot in the physical system with barcodes or whatever that get read for positioning.
As all this parameters do not change over the lifetime of the program, my intention was to design the classes in a way that this properties are readable by getter functions but there should not be any setter functions (maybe not even private ones) in the object o the values cannot be changed by accident.
There is one class/function that reads the config-file and generates the data structure for the rows and columns with the slots. This function should be able to read the config and create objects for every column holding a row of slots and pass all the values from the config down to the slot.
Later when the program is running I also need some way to search for the best matching slot to add the next parcel or for searching parcels and unload them in a certain sequence.
So the (simplfied) basic structure of the classes would be like this:
Class Parcel {
int width;
int height;
int length;
int weight;
}
Class Slot {
vector<Parcel> parcel;
int min_width;
int max_width;
int min_height;
int max_height;
int min_length;
int max_length;
int max_total_weight;
int act_total_weight;
int total_length;
int free_length;
}
Class Row {
vector<Slot> slot;
int row_id;
int max_total_weight;
int act_total_weight;
}
Class Column {
vector<Row> row;
int column_id;
int max_total_weight;
int act_total_weight;
}
Class Storage {
vector<Column> column;
}
So here are my thoughts about how to initialize the data structure:
First possibility would be to pass all the properties in the constructor(s) of the classes, but then the constructors has some huge parameter lists specially for the Slot class that has a lot of properties.
Second thing that came to my mind (and currently my fafourite way to go) is to use config-data-structures that hold all the parameters. This parameter-objects get filled by the config-function and passed to the constructor when initializing the class. Then it also may be useful to use the parameter class as such and not having all the parameters defined in the storage class once more.
Third way is to use private setter and public getter and make the config class friends with the data structure classes to be able to access the setter functions (but i would prefer to have no setters at all in the final storage structure classes.
Fourth way that i was thinking off, was to derive child classes from the structure classes that hold the setter functions (and also some other logic needed for creating the data structure) so the child has no own variables but only additional functions. So the child class is used to fill the properties but the base class gets added to the data structure vector.
I also want to use Factory pattern to initialize the data structure because the objects have often similar or only slightly different properties. So with the second aproach after creating one row of slots I would maybe want to change the max weight of the slots in that row. Therefore I would need to change the setting in the factory and the factory then fills the parameter data structure differently and passes it to the Slot class. Or is it better to pass the data structure to the factory directly and the factory assigns it but then i think this is not what the factory pattern is meant to be.
I don't know if this is a good aproach or which of the above is best practice.
Or am I missing something and there is a way more convenient solution or this?
Thank you (and sorry if the question is maybe not the way it should be)
When constructing your classes as you describe it you can have a look at the creational design patterns.
Your second proposed solution is almost a builder design pattern. This will help you to construct the Slot e.g. piecewise.
As an example:
#include <iostream>
class Slot {
public:
int GetMinWidth() const { return min_width_; };
int GetMaxWidth() const { return max_width_; };
// Builder class
class SlotBuilder {
public:
SlotBuilder& SetMinWidth(int min_width) {
min_width_ = min_width;
return *this;
}
SlotBuilder& SetMaxWidth(int max_width) {
max_width_ = max_width;
return *this;
}
Slot Build() {
return Slot(min_width_, max_width_);
}
private:
int min_width_{/* you can add default value here*/};
int max_width_{/* you can add default value here*/};
};
// This is optional, but creates a neat interface
static SlotBuilder Create() {
static SlotBuilder slot_builder;
return slot_builder;
}
private:
// Make constructor private to restrict access and force the use of the builder
Slot(int min_width, int max_width) : min_width_(min_width), max_width_(max_width) {}
const int min_width_;
const int max_width_;
// .
// .
// Continue with your const attributes
};
int main() {
// Create the slot with the builder
Slot slot = Slot::Create()
.SetMinWidth(10)
.SetMaxWidth(20)
.Build();
std::cout << slot.GetMinWidth() << ", " << slot.GetMaxWidth();
return 0;
}
You can see the example working here
For having different types that are almost the same a Prototype pattern could work if you want to "clone" a class or in your case a Factory pattern could do the job.
There is never an ideal solution or that one pattern that solves it all, so I can't give you a definitive answer, but here are some collected thoughts:
Default values
Primitive types like int don't have a default value, so make sure you give them one explicitly:
struct Parcel {
int width{};
int height = 0;
int length = {};
int weight{};
}
All those different versions above are equivalent, but you really should use one of them. Otherwise you will probably run into UB down the line.
Const correctness
One thing that I love about C++ and that I dearly miss in languages like C# is const correctness. If you want an object to be immutable, declare it as const. To prevent changes to your objects, either instantiate the object as a const:
const Parcel x;
x.width = 10; // compiler error
or make the members of your classes const:
struct Parcel {
const int width{};
const int height{};
const int length{};
const int weight{};
};
Parcel x;
x.width = 10; // compiler error
Aggregate initialization
If you keep your types simple enough you can initialize the class members with curly braces directly:
const Parcel x { 1, 2, 3, 4 };
In C++ 20, you can also name the members, so this code is equivalent to the line above:
const Parcel x { .width = 1, .height = 2, .length = 3, .weight = 4 };
Note that this can bite you later though if you have to deal with ABI stability and versioning. In that case you are better off using getter and setter functions, because that allows you to still change the data layout of your members.
I have to think about design patterns a bit more. I'll update this post if something useful comes out of it.

Can a C++ class's default value be changed at runtime?

In C++ is it possible to change the default values of a class so that all future objects made of that class will have the values specified?
I am trying to make a class that is user-defined at run time that should function nearly identical to other child of the same parent class, but I am struggling with a way to have the same format constructor exist across both. The only way I can think to properly funnel the correct information to every new object is to have either the class type be always treated differently on creation (always put in the user inputs into the constructor) or have the class's object behavior on creation change to default to the inputs defined.
Edit: To better explain the issue and address the XY problem possibility here is the scenario:
General Program:
I want to have the user first define a custom shape by providing a name and the number of lines that define it. The user then can add triangles, squares and the custom shape to their "cart". Each custom shape is the same shape that they specified at the start, the user does not change it during the program. The program could then return general information such as the number of members of a specific type that exists or the total number of lines in the cart.
There is a parent class:
Class Shape
member numLines, name;
And three classes are children of Shape:
Class Triangle
overwritten numLines = 3, name = "triangle";
...
Class userShape
overwritten numline = ????, name = ????;
When I create methods for the cart that interact with "shape" objects I would like to be able to have the same code across the board for creating additional objects, instead of needing to treat the userShape's constructor differently.
From the answers I have received, the static type seems to best fit the user-setting of the default values but I am entirely open to better ways to implement something like this.
Just because you can, doesn't mean you should. Now that I got that out of the way, here's how:
#include <iostream>
struct C {
static int default_val;
C(int i = default_val) {
std::cout << i << '\n';
}
};
int C::default_val = 0;
int main() {
C c;
C::default_val = 1;
C c2;
return 0;
}
A default argument to a function doesn't have to be a constant expression, it just has to be "available" at every place the function is called. And a static variable is just that.
It also doesn't have to be accessible at the call site. So if you want the class to control it, but not have it be modifiable from outside the class, you can make it private.
I would recommend following the next pattern:
Run your application
Read/Load the configuration
Create a factory for that object from that configuration
Use the factory to create more instances of the same object throught the lifetime of your application
For example:
struct Object {
Object(int x, int y) : _x(x), _y(y) {}
int _x, _y;
};
struct DynamicFactory {
DynamicFactory(int x, int y) : _x(x), _y(y) {}
std::shared_ptr<Object> generate() { return std::make_shared<Object>(_x, _y); }
int _x, _y;
};
int main(){
// Load configuration
// Initialize dynamic factory using configuration
DynamicFactory factory(1, 3);
// Generate objects
auto obj1 = factory.generate();
auto obj2 = factory.generate();
return 0;
}
Try to use static members so these are shared across the objects.

Increment non-static variable via static function

I am making an ecosystem in C++ where every ANIMAL on my playing_field will grow_older() at the end of the year.
class ANIMAL
{
private:
int age;
public:
ANIMAL()
{
age=0;
}
static void grow_older(){ age++; }
};
I know that static methods can only use static data, but is there any elegant way to have something similar to an ANIMAL.grow_older();? I could iterate through my playing_field and call grow_older(); on each animal, but I was hoping there is some concept out there that I'm not aware of that avoids iterating through every instance of ANIMAL explicitly.
There's no elegant way to increate the value of data members from many objects at once, so your options seem to be to either have a single static age (which I assume is not valid for your use case), to increase the age of all objects one at a time (by iterating through whatever container they're in to find each), or adopting a different solution completely.
One possible alternative solution is to not store age as a data member at all, but rather store a time_of_birth as a data member, and also keep track of a current_time as a static data member. Your class would then look something like:
class ANIMAL
{
private:
int time_of_birth;
static int current_time;
public:
ANIMAL()
{
time_of_birth=current_time;
}
static void grow_older(){ current_time++; }
int age() { return current_time - time_of_birth; }
};
int ANIMAL::current_time = 0; /* defined in a source, not header file */
If each time you must increase the age you also need to examine the age (for example, to see if the animal must now be dead), there probably is no savings since you need to iterate through your menagerie anyway. On the other hand, if you do not need to examine the age as (or more) often than you need to increase it, this approach should perform better due to resulting in less work.

C++ Getting an object

Lets say I have the following class:
static int counter = 0;
class Account {
public:
int ID;
int favNumber;
Account(int favNum) {
this->ID = ++counter;
this->favNumber = favNum;
}
};
Account user1(4);
Account user2(9);
Now both accounts user1 and user2 have different ID that is unique. Is there any way by knowing the ID of the account get the field of the object like "favNumber", if so how should it be implemented?
Something like getFieldById(int ID)
You may use std::map to do this :
#include <map>
class Account {
// Make attributes private. It is a better practice
int ID;
int favNumber;
static int s_Counter;
//^^^^^^^^^^^^^^^^^^^^^ It is better to move it as a static private member of Account
public:
Account(int favNum) {
this->ID = ++s_Counter;
this->favNumber = favNum;
}
// GETTERS
int GetFavNumber() const { return favNumber; }
int GetID() const { return ID; }
};
int Account::s_Counter = 0;
// ^^^^^^^^^^^^^^^^^^^^^^^^ Don't forget to initialize it
Account user1(4);
Account user2(9);
std::map<int, Account*> accounts;
accounts[user1.GetID()] = &user1;
accounts[user2.GetID()] = &user2;
// To get a favNum with some id :
accounts[id]->GetFavNumber();
But with this technique, be sure that the pointers are still valid ! If not, you could have bad surprises ...
What we have done in this previous code ?
We passed the attributs in private (better practice).
We created Getters to access them.
We passed the counter static variable as a static private member of Account.
We used std::map to have a listing of the accounts created and the keys are the IDs of the Accounts.
You can use
std::map<int, Account*>
to store a pointer to the accounts by their id. It's up to you to make sure the pointers remain valid. Alternatively, you could use
std::map<int, Account>
and let the map look after your accounts for you.
you can create a list and for each time you pass the constructor add the item to the list. then when a request get to your getFieldById search your list.
the list will have to be in a place you can search in and only be initiate once
You would need to central place to store all the objects that are going to be created and then search for the id there.
You could store them as
Plain old array
Search the entire list for your object of ID and then return the field
ID indexed array
array[ID] is the object you need, return the field
Hash(std::map) from ID to object
Similar syntax as ID indexed array but is a hash table lookup
Each have their pros and cons in simplicity, speed of search, memory used etc.
You could also store object pointers in the above.
To automate things, you can make the above list a private static member of your Account class, and add to it in the constructor.

Efficient way to generate id unique to class?

Is there any efficient way in C++ of generating an ID unique to the class, not to the instance? I'm looking for something of this level of simplicity (this generates an ID for every instance, not for every class type):
MyClass::MyClass()
{
static unsigned int i = 0;
id_ = i++;
}
Edit: Why I want unique IDs.
I'm writing a game. All entities in my game will have different states they can be in (walking left, jumping, standing, etc); these states are defined in classes. Each state needs to have its own ID so I can identify it.
You can try this, but it's not-deterministic.
int id_count = 0;
template <typename T>
int get_id()
{
static int id = id_count++;
return id;
}
Then just use:
get_id<int>(); // etc.
Of course, this isn't thread safe.
Again, it's not deterministic: the IDs are generated the first time you call the function for each type. So, if on one run you call get_id<int>() before get_id<float>() then on another run you call them the other way round then they'll have different IDs. However, they will always be unique for each type in a single run.
Basically you are asking for a custom rolled RTTI solution, that you can selectively apply to classes.
This can start from very crude preprocessor stuff like :
#define DECLARE_RTTI_CLASS(a) class a { \
inline const char * class_id() { return #a };
.. to a more sophisticated solutions that track inheritance etc, essentially partially duplicating compiler RTTI functionality. For an example, see Game Programming Gems #2, Dynamic Type Information
Previous discussions on gamedev on the same subject are also worth reading
Use your MyClass as a primitive, and incorporate a static instance of one into each class you want to ID.
class MyOtherClass1 {
static MyClass id;
};
class MyOtherClass2 {
static MyClass id;
};
[etc.]