+ 13
Object properties and Methods
So if i have an object,is there anyway of getting/outputting all its properties and methods? This is very easy in js,but the solution seems unclear in other languages. Ill appreciate it if solution could be orovided in: java c++ Thanks
15 odpowiedzi
+ 5
Brains in c++ you can make this bcoz you just required class name in that and the object name which you want to prefer....
+ 5
Manual you are right
+ 5
Brains
In java, you can use java.lang.reflect class to access the methods called:
getDeclaredFields() -> to access all public properties.
getDeclaredMethods() -> to access all declared methods.
Here is an example:
https://code.sololearn.com/c1ZsTEPMhher/?ref=app
+ 4
basically in c++ objects properties are held by using methods so..methods(class) is used to express the object within it .
+ 4
in c++ you can‘t if you don‘t already know the inner workings(aka the declaration) of you class. you can get all the data in the class because its just saved from &my_object to &my_object + sizeof(my_object)(not quite sure if sizeof works at runtime). i dont see an obvious way to also get the type information or differentiate between different members. maybe you could do something like this using compiler specific implementations of rtti, but thats really complex and it doesn‘t save information about the members as far as i know bit here is an article about rtti: http://www.openrce.org/articles/full_view/23
+ 4
So provided i have no idea which class an object came from,and i need to extract all its properties,its impossible in this languages?wow
+ 4
Brains
I believe that is right, in c++ you need to know exactly where it is came from.
If private you need a method to get past encapsulation.
java and c# share that feature.
+ 4
Brains you might be able to find out which class it is from using typeid(). but c++ and other compiled languages normaly dont support this stuff since it costs alot of performance. also check out this article http://jackieokay.com/2017/04/13/reflection1.html
+ 4
The thing is im creating random objects from random classes.
if class A had a property called "big" i could test if that object had a big property to find out whether its from class A.good idea,not when you have several classes that share similar properties,thats why im desperate for this
+ 4
edit: mentioned by Max
typeid example
example from a website - not sure where
https://code.sololearn.com/cGdQSt426qOe/?ref=app
+ 4
Thanks Manual this is way easier
+ 3
Brains
C++
if public you can just call them.
cout << obj.var;
if private
you need to call or return from a method, definded within the class.
dataType getter(){
return var;
}
+ 3
Manual but that doesn‘t automaticaly iterate through all members like he asked for.
+ 3
@Brains in that case use typeid or something that description makes your problem simpler than i thought, i thought you didnt even have the declarations if the classes
+ 1
In python we have read only attribute __dict__ . which contains all details like class name ,variable of object,methods of objects. Any thing else in python which gives object properties?