Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts

    Property Name Same as Method Name?

    I get a compile-time error doing:
    Code:
    class Foo {
       private:
          int length;
       public:
          Foo(int x) {
             length = x;
          }
          int length() {
             return length;
          }
    }
    How can I have the length property and the length() function have the same name?

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    No, it doesn't work that way, as your compiler already pointed out. Conventionally people prefix the method name with get_/get/Get.

  • Users who have thanked ralph l mayo for this post:

    Trinithis (10-15-2007)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Properties hold values (NOUNS), methods perform actions (VERBS). Although the English language has a few words that can be both nouns and verbs, programming languages do not.

    In any case is the length() method supposed to be setting the length or getting the length, the name doesn't say.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Or you could prefix all your member variable names with 'm_' that way the function doesn't have the same name.
    OracleGuy

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    UK
    Posts
    177
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I tend to place g_ (C++) or just _ (C#) in front of globals.

    Stu
    if ($ENV{'QUERY_STRING'} eq "Afrow UK") {
    print "$ENV{'QUERY_STRING'} rocks!";
    } else {
    print qq~$ENV{'QUERY_STRING'} sucks :)~;
    }

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Location
    Minneapolis, MN
    Posts
    208
    Thanks
    0
    Thanked 1 Time in 1 Post
    In Ruby it would be as easy as this
    Code:
    class Person
      def initialize(name)
        @name = name.capitalize
      end
    
      def name=(name)
        @name = name.capitalize
      end
     
      def name
        puts @name
      end
    end
    
    me = Person.new("Anthony")
    me.name
    me.name = "Elizabeth"
    me.name
    Output
    Code:
    Anthony
    Elizabeth
    anthony


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •