Let me give an idea of what I wish to do: I have a structure or class called student
, which contains variables like
int roll_no
and
int reg_no
If the user wishes to add a new variable like char name
at run time how can it be done?
Let me give an idea of what I wish to do: I have a structure or class called student
, which contains variables like
int roll_no
and
int reg_no
If the user wishes to add a new variable like char name
at run time how can it be done?
Based on the word "Structure" and the variable declarations, I'm going to guess this question is about some flavor of C. How exactly to do this will depend on the language, but as a general rule, if the language is compiled (e.g. C/C++, Java), this is not possible. If the language is interpreted (e.g. Python), this might sort of be possible, like this:
Here we've added the
name
attribute to thea
object only, and not the entire class. I'm not sure how you're go about that for the whole class.But really, the answer to your question is "Don't". You should think about your program and the objects you're using enough to know what fields you will and won't need. If you'll want to have a name field at some point in your program, put it in the class declaration. If you don't want it to have a value on object creation, use a sensible default like
null
.Edit
Based on your comments, there are a couple of ways to approach this. I'm still not entirely clear on what you want, but I think one of these cases should cover it. Of the languages I know, Python is the most flexible at runtime:
Python
In Python, a class is just another kind of object. Class variables (check out this question too) belong to the class itself, and are inherited by any instances you create:
You can use this to add attributes to every instance of your class, but they will all have the same value until you change them. If you want to add an attribute to just one instance, you can do this:
One drawback to using Python is that it can be kinda slow. If you want to use a compiled language it will be slightly more complicated, and it will be harder to get the same functionality that I mentioned above. However, I think it's doable. Here's how I would do it in Java. The implementation in C/C++ will be somewhat different.
Java
Java's class attributes (and methods) are called (and declared)
static
:static
variables are normally accessed through the class name, as in Python. You can get at them through an instance, but I believe that generates a warning:You can't add attributes to a Java object at runtime:
However, you can have a
HashMap
attribute,static
or not, which you can add entries to at runtime that act like attributes. (This is exactly what Python does, behind the scenes.) For example:And then you can do things like:
Notice that I did not declare
att
static
above, so in this case each instance of theMyObj
class has this attribute, but the class itself does not. If I had declared itstatic
, the class itself would have one copy of this hash. If you want to get really fancy, you can combine the two cases:There are a few details I've left out, like handling the case of the
HashMap
s not having the value you're asking for, but you can figure out what to do there. As one last note, you can do in Python exactly what I did here in Java with adict
, and that might be a good idea if the attribute names will be strings. You can add an attribute as a string in Python but it's kind of hard; look at the documentation on reflection for more info.Good luck!