Thread: couple basic questions about signatures

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    couple basic questions about signatures

    I took this example from http://publib.boulder.ibm.com/infoce...ef/cplr318.htm

    Code:
    // This example illustrates overloading the plus (+) operator.
    
    #include <iostream>
    using namespace std;
    
    class complx
    {
          double real,
                 imag;
    public:
          complx( double real = 0., double imag = 0.); // constructor
          complx operator+(const complx&) const;       // operator+()
    };
    
    // define constructor
    complx::complx( double r, double i )
    {
          real = r; imag = i;
    }
    
    // define overloaded + (plus) operator
    complx complx::operator+ (const complx& c) const
    {
          complx result;
          result.real = (this->real + c.real);
          result.imag = (this->imag + c.imag);
          return result;
    }
    
    int main()
    {
          complx x(4,4);
          complx y(6,6);
          complx z = x + y; // calls complx::operator+()
    }

    1. What does it do when assigning the values to variables inside the constructor? complx( double real = 0., double imag = 0.);
    2. What does it do when having const infront of an argument? (const complx&)
    3. What does it do having const after a signature? (const complx&) const; by the way is this still considered part of the signature?
    4. Is there such a thing as returning a const?

    I felt anyone of these questions would be unworthy of an entire thread so I put them in one. Hopefully I'm not breaking the rules for this.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You only ask these questions because you are using a reference as an introductory text. References will organize their content differently. While I won't say you can't use a reference like a textbook, you do have to read them a certain way to get the most out of it. You wouldn't learn anything comprehensively only reading starting on page one.

    C++ Programming/Classes - Wikibooks, collection of open-content textbooks - (explains constructors, const methods, and a host of other things in detail)
    C++ Programming/Programming Languages/C++/Code/Statements/Variables/Type - Wikibooks, collection of open-content textbooks - (more explanation on the const modifier)

    But you might just want to start at the beginning chief.
    Last edited by whiteflags; 09-30-2010 at 01:59 AM.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107

    A couple replies to the questions...

    c_weed,


    Quote Originally Posted by c_weed View Post
    1. What does it do when assigning the values to variables inside the constructor? complx( double real = 0., double imag = 0.);
    2. What does it do when having const infront of an argument? (const complx&)
    3. What does it do having const after a signature? (const complx&) const; by the way is this still considered part of the signature?
    4. Is there such a thing as returning a const?

    I felt anyone of these questions would be unworthy of an entire thread so I put them in one. Hopefully I'm not breaking the rules for this.
    Worthy or not, they're here....

    Reply 1. These are default values for the parameters. If you do not include the parameters when you instantiate a complx, they are "assumed" to be zero. If you only include one parameter the following assume their default values, i.e. complx a( 5 ) produces a complx where i has a zero coefficient. Notice that the default values only occur in the declaration of class complx, not in the implementation (definition) of the member functions....

    Reply 2. When const is placed in front of an argument in that manner one may assume that the value of the argument may not change during execution of the function. In fact, the compiler will enforce such.

    Reply 3. When const is placed after a member function's signature it means that the value of the instance (object) calling the function will not change during it's execution, with the exception of mutable (keyword hint there) variables. Once again, the compiler will enforce this.

    Reply 4. There are occasions where the return value of a function is constant, and thus it is qualified as const in the signature. For example, const int foo( const char *s ), the resulting value is to be considered constant by the caller (client)...I'm struggling to find a simple example of that situation at the moment though. Often constant return values occur for "get" type member functions sharing member data like strings (or char*)...at least that has been my experience.

    As was mentioned a moment ago, a good exercise book can be priceless, but a language specification would go along really well with one of those reference books, once you can read formal language specifications.

    Best Regards,

    New Ink -- Henry
    Last edited by new_ink2001; 10-01-2010 at 12:41 AM. Reason: brevity
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    As was mentioned a moment ago, a good exercise book can be priceless, but a language specification would go along really well with one of those reference books, once you can read formal language specifications.
    Just to get my take on it I find actually owning the language specification to be completely useless. Even in the event where I have a question pertaining to the Standard, the document, it is enough for me to find and peruse the HTML versions. And I frequently don't have questions like that. In my experience here, even, people pull out Chapter and Verse only to end a stupid argument.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by new_ink2001 View Post
    Reply 4. There are occasions where the return value of a function is constant, and thus it is qualified as const in the signature. For example, const int foo( const char *s ), the resulting value is to be considered constant by the caller (client)...I'm struggling to find a simple example of that situation at the moment though. Often constant return values occur for "get" type member functions sharing member data like strings (or char*)...at least that has been my experience.
    Returning const references is very common.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 04:55 PM
  2. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  3. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 04:06 PM
  4. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  5. couple questions for newb
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2001, 03:32 AM