Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

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.

like image 682
Chris Kuchman Avatar asked Jan 03 '23 08:01

Chris Kuchman


2 Answers

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.

like image 55
StoryTeller - Unslander Monica Avatar answered Jan 18 '23 00:01

StoryTeller - Unslander Monica


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;
}
like image 20
Daniel Trugman Avatar answered Jan 18 '23 02:01

Daniel Trugman