Thread: Accessor Methods vs Jump to the code | String const vs String

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    122

    Accessor Methods vs Jump to the code | String const vs String

    Hello guys,
    I'm learning c++ and I'm trying to implement it on the correct way.
    However, I found different approachs, and sometimes with opposite directions.

    I'm using this "standard" class implemention, which is described on the c++ annotations .
    Code:
        #include <string>
    
        class Person
        {
            std::string d_name;         // name of person
            std::string d_address;      // address field
            std::string d_phone;        // telephone number
            size_t      d_mass;         // the mass in kg.
    
            public:                     // member functions
                void setName(std::string const &name);
                void setAddress(std::string const &address);
                void setPhone(std::string const &phone);
                void setMass(size_t mass);
    
                std::string const &name()    const;
                std::string const &address() const;
                std::string const &phone()   const;
                size_t mass()                const;
        };
    Of course, that I'm using namespace std , but I follow this "prescription".

    What do you think about it? Is it worthing?
    Should I use string const with a pointer or simply use the string type passing values instead of references?

    Do I really need set_variables? Probably this turns everything more difficult when I'll try to handle a different type of constructor (like I describe in this post Constructors and Conditionals)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marcoesteves
    What do you think about it? Is it worthing?
    It looks okay, though you probably want to provide a default constructor that does something more reasonable than just default construct everything, or disable it. Oh, and then you might want to provide a constructor to initialise the variable member variables appropriately.

    Quote Originally Posted by marcoesteves
    Should I use string const with a pointer or simply use the string type passing values instead of references?
    Do not use pointers here. Using pass by const reference is fine.

    Quote Originally Posted by marcoesteves
    Do I really need set_variables? Probably this turns everything more difficult when I'll try to handle a different type of constructor
    You do not necessarily need a setter for each member variable (and in fact if all your setters do nothing more than return the member variable, then you don't gain much over just having public member variables). It depends on what exactly you are trying to model.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying const string
    By kunalnandi in forum C Programming
    Replies: 2
    Last Post: 03-12-2012, 04:48 AM
  2. string methods hard time
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2006, 10:00 AM
  3. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  4. String methods
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 03-10-2002, 11:39 AM

Tags for this Thread