1

So, let's say, in a class in C++, I have a variety of member variables. Structs, strings, ints, etc. etc. Could be anything. These variables can or cannot be set by the initialization of the object of this class. Given int a, float b, char c, sometimes all of them or none of them can be set. When they are set, they can be set to any possible value of the variable. I would like to find someway of setting, and determining whether or not a variable has been set without:

1) Lots of casting. I could always create a Data_Value decorator class that has a boolean, and template it to whatever the given variable is. This would require calling a_data_value.value and a_data_value.isInitialized.

2) Lots of extra Boolean variables. I'd rather not have bool a_initialized, bool b_initialized.

What I would really like to do is something like this:

Python add to a function dynamically

in C++, with any and all variables, including primitives. Tall order I know, and I'm fully expecting the pessimistic answer.

Community
  • 1
  • 1
zabuni
  • 198
  • 7
  • 1
    Do you have a problem with using the constructor for this? – JoshD Oct 05 '10 at 18:35
  • Do you need to use C++ for this project? If a dynamic language (or a language with introspection) is better suited to the problem, maybe the best approach is to use one. – nmichaels Oct 05 '10 at 18:35
  • If you could elaborate on why you want this, perhaps we could provide alternative solutions. – JoshD Oct 05 '10 at 18:45
  • Is there a reason why you can't simply initialize all your variables on allocation? – jalf Oct 05 '10 at 18:47
  • Your linked Python question does not seem to have anything at all to do with this question. More details on why its relevant would be good. – Zan Lynx Oct 05 '10 at 18:52
  • What do you mean by "non-object variable"? Do you mean non-class? But that would exclude "Structs, strings". Do you mean what you write? But that would just leave references. Can you please clarify? – Johannes Schaub - litb Oct 05 '10 at 20:32
  • The Python link deals with inserting functions and variables into already existing classes. If I could do that, I could add an isInitialized method to the C++ class. As for non-objects, I meant primitives, sorry. – zabuni Oct 05 '10 at 21:16

4 Answers4

2

You're right. It's impossible to determine at runtime whether a primitive is "set". Some compilers will warn you for some cases of using uninitialized values, but this is not at all guaranteed.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • ...because all primitives have *some* value all the time. They *don't* have a set/non-set state. Actually all the data members of a class or structure have this property too, but you can build-in a set-ness flag if you need it. – dmckee --- ex-moderator kitten Oct 05 '10 at 20:27
  • @dmckee, that's why "set" is in quotes. Specifically, uninitialized variable have indeterminate value (§8.5/11), and it's impossible to determine if a value is indeterminate. :) – Matthew Flaschen Oct 06 '10 at 02:52
1

I would use a nullable template. See http://www.codeproject.com/KB/mcpp/CNullable.aspx

Josh
  • 2,259
  • 4
  • 22
  • 25
0

Suppose you have

class Bob {
 int a;
 int b;
 double c;
 complex<double> d;
 Bob () : a(), b(), c(), d() {}
};

When you create a new Bob, everything will be set to default (zero in this case).

There is no set or not set state for primitive types. They always hold some value.

JoshD
  • 12,490
  • 3
  • 42
  • 53
0

If you want to rewrite Python in C++ you can.

You'd need an efficient unordered_map class keyed by string. The string would be the variable name.

Each variable value would be a class (call it a VARIANT, heh) that can hold any primitive value.

Then instead of a C++ struct you'd make your "struct" be an instance of your unordered_map aka dictionary.

If the variable name is found in the dictionary then it was set and you can return the value. If it isn't found it was never set.

If you plan to reference your dictionary keys by name from within C++ you will want to use the following for efficiency:

Instead of:

VARIANT v = dict["name"];

Use:

static const std::string name_key("name");
VARIANT v = dict[name_key];

That way instead of building a std::string containing "name" for the key lookup every time into the function, it will be done once.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131