Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
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:
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
 

Lyle

macrumors 68000
Jun 11, 2003
1,874
1
Madison, Alabama
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?
No, there's not a way in standard C++ to simply declare an attribute and get getter and setter methods for free (as you can with Ruby's attr_accessor). You must write those accessor methods "by hand".

toddburch said:
Or, should I just delcare the C++ member variable "var" as public and be done with it?
You could do that, but it's a bad habit to get into. There are a number of advantages to encapsulation (hiding the data behind accessor methods), such as being able to set a debugger breakpoint inside the setter method to determine when the variable's value is being changed. My advice is to take the time to do it the right way.
 

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
You could do that, but it's a bad habit to get into. There are a number of advantages to encapsulation (hiding the data behind accessor methods), such as being able to set a debugger breakpoint inside the setter method to determine when the variable's value is being changed. My advice is to take the time to do it the right way.

Excellent advice. Thanks. Will do.

Todd
 

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
OK, I'm coding up these setters and getters. What do people use for a namining convention?

For instance,
Code:
class File { 
   string name ; 
public: 
   void setname(string s) ; 
   string getname() ; 
} ; 

void setname(string s) { 
   name = s ; 
} 

string getname() { 
   return name ; 
}

Now, I have 15 other variables to add, so that's 30 member functions to declare and 30 member functions to write and 30 function names to derive in a meaning full manner.

Surely there's an less knuckle dragging way to do this. :(

Or, is this just death by encapsulation?

Todd
 

Lyle

macrumors 68000
Jun 11, 2003
1,874
1
Madison, Alabama
OK, I'm coding up these setters and getters. What do people use for a naming convention?
If it were Java code, the established convention would be to use "CamelCased" method names like getName() and setName(). For C++ there's not really a standard per se; I'd go either with the Java style, or with words separated by underscores, e.g. get_name() and set_name().

toddburch said:
Now, I have 15 other variables to add, so that's 30 member functions to declare and 30 member functions to write and 30 function names to derive in a meaning full manner.

Surely there's an less knuckle dragging way to do this. :(

Or, is this just death by encapsulation?
If you're using a programming editor worth its salt, you should be able to create a macro of some kind that will let you type in the attribute name (e.g. "name") and then it will generate the code for you.

As your design evolves, you might also consider whether some of those fifteen data members can be extracted into separate classes. See "Extract Class" and "Introduce Parameter Object" in Martin Fowler's Refactoring.
 

sord

macrumors 6502
Jun 16, 2004
352
0
As far as naming convensions, I typically stick to first word lowercase, the rest with a first capital letter (so setName, getName, setPhoneNumber, getPhoneNumber, etc) -- though this is of course just a matter of opinion.

As far as needing to create all of those accessors, it depends on what tool you are using. Some IDEs have support for generating them for you (such as Eclipse does with Java).
As a worst case scenario, you could always fire up Ruby or something, make an array of your variable names, and have it generate the code for you for the getters/setters. Then just copy your output and drop it in your C++ program.

Also, I noticed your code for your accessors wasn't actually in the class, you want to do:
Code:
void File::setName(string s) { 
   name = s ; 
} 

string File::getName() { 
   return name ; 
}

EDIT: darn - was beat to it. I second the suggestion that if you have that many members you may want to look into refactoring.
 

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
If it were Java code, the established convention would be to use "CamelCased" method names like getName() and setName(). For C++ there's not really a standard per se; I'd go either with the Java style, or with words separated by underscores, e.g. get_name() and set_name().

Underscores it is.

If you're using a programming editor worth its salt, you should be able to create a macro of some kind that will let you type in the attribute name (e.g. "name") and then it will generate the code for you.

Xcode. Is it worth it's salt?

As your design evolves, you might also consider whether some of those fifteen data members can be extracted into separate classes. See "Extract Class" and "Introduce Parameter Object" in Martin Fowler's Refactoring.

Yes, some of them are classes themselves I have defined. Others are vectors, and other types and containers.

I'll look into the Refactoring book. Thanks for the links.

Todd
 

toddburch

macrumors 6502a
Original poster
Dec 4, 2006
748
0
Katy, Texas
As far as needing to create all of those accessors, it depends on what tool you are using. Some IDEs have support for generating them for you (such as Eclipse does with Java).
As a worst case scenario, you could always fire up Ruby or something, make an array of your variable names, and have it generate the code for you for the getters/setters. Then just copy your output and drop it in your C++ program.

True on the ruby hack. I'm using XCode for the C++.

Also, I noticed your code for your accessors wasn't actually in the class, you want to do:...

Oops. Typo. I should have cut and pasted my code instead of reinventing and getting it wrong.

Thanks. Todd
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.