-
May 27th, 2013, 09:31 AM
#1
Changing an object's property through console
Working on a console application and I am trying to figure out a way that allows users to enter an object property along with a value. For example
HTML Code:
class Box{
public:
int height;
int width;
int length;
};
int main(){
Box ab;
string Name,value
cin>>Name>>value;
map<string,string> propMap
propMap[name] = value;
}
For example if a user inputs "height" for Name and "13" as value I would like to find a way to change ab's height to 13. I want to make this work so that one can add another class and get the same functionality.
I tried looking online and I think maps are a way to do this and I tried doing that but I don't know how to proceed after/what code to write that would allow me to change it.
Note: I can't use if else statements or hardcoding to match the input to member as I want to make this extensible.
-
May 27th, 2013, 02:26 PM
#2
Re: Changing an object's property through console
Why do you want to do this? What are you really trying to achieve?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 27th, 2013, 04:26 PM
#3
Re: Changing an object's property through console
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
-
May 27th, 2013, 06:22 PM
#4
Re: Changing an object's property through console
Originally Posted by sunnykumar4434
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
We know that.
I'll try to reask the question:
At a higher-level, what are you trying to achieve? What real-world problem (forget about the language that you're using) are you trying to solve? Whatever it is, there are more than likely better paradigms and design patterns that can be used. In C++, there is no standard way for you to write a class at compile time and expect the runtime environment to know what the member variable names are.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 27th, 2013 at 10:34 PM.
-
May 27th, 2013, 11:17 PM
#5
Re: Changing an object's property through console
Originally Posted by Paul McKenzie
We know that.
I'll try to reask the question:
At a higher-level, what are you trying to achieve? What real-world problem (forget about the language that you're using) are you trying to solve? Whatever it is, there are more than likely better paradigms and design patterns that can be used. In C++, there is no standard way for you to write a class at compile time and expect the runtime environment to know what the member variable names are.
Regards,
Paul McKenzie
It's a part for an assignment for my class and one of the questions is to achieve such a functionality.
-
May 27th, 2013, 11:50 PM
#6
Re: Changing an object's property through console
Originally Posted by sunnykumar4434
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
I suspect that instead of "class", you mean "object", i.e., you want to write code such that given an object of the Box class, the member variables of the object can be changed depending on the user input. You can store these objects in a map, and thus the user chooses the object to change based on the key. Is this what you are thinking of?
-
May 28th, 2013, 12:03 AM
#7
Re: Changing an object's property through console
Originally Posted by laserlight
I suspect that instead of "class", you mean "object", i.e., you want to write code such that given an object of the Box class, the member variables of the object can be changed depending on the user input. You can store these objects in a map, and thus the user chooses the object to change based on the key. Is this what you are thinking of?
Yes, how would I go on doing that?
-
May 28th, 2013, 12:10 AM
#8
Re: Changing an object's property through console
Oh, in that case, you have little choice other than to use "if else statements or hardcoding" (e.g., equivalently, a switch), unless you can change the class definition. If you can change the class definition, then I would go for a std::map<std::string, int> member (or std::unordered_map), seeing that the current members are all ints.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|