Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 6, 2007, 10:20 AM   #1
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
C++ vs Ruby: setters and getters

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
toddburch is offline   0 Reply With Quote
Old Nov 6, 2007, 11:49 AM   #2
Lyle
macrumors 68000
 
Lyle's Avatar
 
Join Date: Jun 2003
Location: Madison, Alabama
Quote:
Originally Posted by toddburch View Post
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".

Quote:
Originally Posted by toddburch
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.
Lyle is offline   0 Reply With Quote
Old Nov 6, 2007, 11:54 AM   #3
toddburch
Thread Starter
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
Quote:
Originally Posted by Lyle View Post
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 is offline   0 Reply With Quote
Old Nov 6, 2007, 12:35 PM   #4
toddburch
Thread Starter
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
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
toddburch is offline   0 Reply With Quote
Old Nov 6, 2007, 01:22 PM   #5
Lyle
macrumors 68000
 
Lyle's Avatar
 
Join Date: Jun 2003
Location: Madison, Alabama
Quote:
Originally Posted by toddburch View Post
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().

Quote:
Originally Posted by toddburch
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.
Lyle is offline   0 Reply With Quote
Old Nov 6, 2007, 01:27 PM   #6
sord
macrumors 6502
 
Join Date: Jun 2004
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.
__________________
12" iBook 1GHz 768MB RAM 60GB HD Airport Extreme [sold and missed]
17" iMac G5 1.6GHz 768MB RAM 160GB HD [sold]
PowerMac Quad 2.5GHz G5 2.5GB RAM
sord is offline   0 Reply With Quote
Old Nov 6, 2007, 01:40 PM   #7
toddburch
Thread Starter
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
Quote:
Originally Posted by Lyle View Post
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.

Quote:
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?

Quote:
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 is offline   0 Reply With Quote
Old Nov 6, 2007, 01:42 PM   #8
toddburch
Thread Starter
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
Quote:
Originally Posted by sord View Post
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++.

Quote:
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
toddburch is offline   0 Reply With Quote
Old Nov 6, 2007, 02:06 PM   #9
Lyle
macrumors 68000
 
Lyle's Avatar
 
Join Date: Jun 2003
Location: Madison, Alabama
Quote:
Originally Posted by toddburch View Post
Xcode. Is it worth its salt?
I've not used Xcode all that much, so I suppose that is an exercise left to the reader.
Lyle is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Wrapping Objective-C library for Ruby moonman239 Mac Programming 1 Apr 25, 2014 07:06 PM
New motherboard inside iMac G3 Ruby Graveyard PowerPC Macs 2 Dec 10, 2012 02:42 AM
Ruby on Rails developing GimmeSlack12 Mac Programming 2 Nov 27, 2012 03:06 PM
Ruby verion shipping with !0.8, what version is it? mikeray OS X 10.8 Mountain Lion 1 Jun 21, 2012 06:46 PM

Forum Jump

All times are GMT -5. The time now is 12:50 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC