Thread: Method Calls another Method

  1. #1
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130

    Method Calls another Method

    Finished reading ch 9 of Programming Principals but I have a question. The part where a default constructor is defined for a class called Date. The constructor initialized three variables that are located in the private sphere of the Date object (year, month, and day). At any rate, these three variables since they are private members, their values are otherwise accessed through acessor methods. During default construction this same method containing a static default value, separately calls each one of these public acessor methods and the return type of this culprit in question is a Date object. The way that it is written in the default construction, this method calls each of the three acessor methods in turn so that all three Date elements are initialized. This looked highly unusual. Why can a method call another method?
    Code:
    Date::Date()
      :y{default_date().year()},
      :m{default_date().month(),
      :d{default_date().day()}
    {
    }
    Also
    Code:
    const Date& default_date() {
      static Date dd {2001, Month::Jan, 1};
      return dd;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by FourAngels
    Why can a method call another method?
    You are looking at an example, of chaining of function calls. A method (or more generally in C++ terminology: a member function) calling another method might look like this:
    Code:
    class X
    {
    public:
        void foo() {}
    
        void bar()
        {
            foo();
        }
    };
    That is, the member function bar calls the member function foo.

    For the chaining of function calls, on the other hand, we see that default_date() in your example returns a const reference to a Date object, so the year, month and day member functions are called on that Date object.

    You may have encountered or will encounter this when overloading operator<< and operator>> for I/O streams.
    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

  3. #3
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    I can see how it works similar to :
    Code:
    Date Example {2010, Month::May, 11};
    std::cout << Example.year() << std::end;
    // default_date().year(); instead of Example.year()
    Okay it was the first time I had ever seen chaining. He did not explain that. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-11-2011, 10:21 PM
  2. class method calls
    By Shingetsu Kurai in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2011, 08:52 PM
  3. difference between this->method() and method()
    By nacho4d in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2009, 04:11 PM
  4. Encrypt method (from decrypt method)
    By mmmmmm in forum C# Programming
    Replies: 3
    Last Post: 09-19-2009, 10:35 AM
  5. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 12:59 AM