C++ vs Ruby: setters and getters

Discussion in 'Mac Programming' started by toddburch, Nov 6, 2007.

  1. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #1
    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
     
  2. macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #2
    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".

    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.
     
  3. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    Excellent advice. Thanks. Will do.

    Todd
     
  4. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    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
     
  5. macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #5
    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().

    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.
     
  6. macrumors 6502

    Joined:
    Jun 16, 2004
    #6
    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.
     
  7. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    Underscores it is.

    Xcode. Is it worth it's salt?

    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
     
  8. thread starter macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    True on the ruby hack. I'm using XCode for the C++.

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

    Thanks. Todd
     
  9. macrumors 68000

    Lyle

    Joined:
    Jun 11, 2003
    Location:
    Madison, Alabama
    #9
    I've not used Xcode all that much, so I suppose that is an exercise left to the reader. ;)
     

Share This Page