I fairly versed in Ruby in regards to class and method definitions, and I'm learning C++ (as I implement a project with it).
In Ruby, you can define instance method setters and getters inside a class for an instance variable, or define an accessor for it to reduce the typing need if all you need is basic setting and getting of a non-public instance variable.
Long way in Ruby with a setter and getter:
Ruby, short way:
For C++, I'm looking for a short way for defining member functions where all I need is Ruby "attr_accessor" type of specification. Is there a way to do this? Or, should I just delcare the C++ member variable "var" as public and be done with it?
Thanks, Todd
In Ruby, you can define instance method setters and getters inside a class for an instance variable, or define an accessor for it to reduce the typing need if all you need is basic setting and getting of a non-public instance variable.
Long way in Ruby with a setter and getter:
Code:
class MyClass
def mysetter(var)
@var = var ;
end # method mysetter
def mygetter()
return @var
end # method mygetter
end # class MyClass
me = MyClass.new()
me.mysetter("MacRumors Rocks")
puts me.mygetter()
Ruby, short way:
Code:
class MyClass
attr_accessor :var
end # class
me = MyClass.new()
me.var = "MacRumors Rocks"
puts me.var
For C++, I'm looking for a short way for defining member functions where all I need is Ruby "attr_accessor" type of specification. Is there a way to do this? Or, should I just delcare the C++ member variable "var" as public and be done with it?
Thanks, Todd