Thread: question regarding consrtuctor calls

  1. #1
    Registered User
    Join Date
    Nov 2005

    question regarding constructor calls

    I am working through a practice exam for a programming final and I have a question about the way a program calls class constructors. How am I able to tell how many times the constructor is called in a specific program? For example...

    int main(){
      String A, B("Examination");
      cout << "Enter a string: ";
      cin >> A;
      cout << "The string you entered was: " << A << endl;
      if(A == "Midterm" || A == "Final"){
        cout << A << " " << B << endl;
    This is main() is based off a String class interface provided on the sample exam by the instructor. There is a question that asks how many calls to the constructor there are. When I look at this, I see 2 for sure and maybe 1. Where String A and B are declared and I believe where the user inputs data into A. Now, according to the sample exam the answer is 4 and I am not really sure why. Is it perhaps that they are called when they are declared and the when they are outputted? That would be 4 but doesn't really seem right to me.
    Last edited by alt234; 12-11-2005 at 03:17 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well I'd say the == also cause temporary String objects to be created out of the "strings", so that the equality overloaded function can be called.

  3. #3
    Registered User
    Join Date
    Jun 2005
    There are difference answers depending on how the String class and various operators are implemented.

    For example, does operator<< accept a reference to String or a String by value? Does the String class support an operator==() that accepts a const char *, or does the comparison rely on creating a temporary String [as suggested by Salem]?

    The working of operator || (i.e. it shortcuts) can also reduce the number of comparisons done and (if the comparison creates a temporary String) will reduce the number of times a constructor is called. Consider what happens if A == "Midterm" returns true.

    These is also an issue of compiler optimisation: a compiler is allowed (by the C++ standard) to avoid creating temporaries if the only way of detecting those temporaries is to monitor calls to constructors and destructors.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Hmm, ok thanks. Here is the interface, we aren't given an implementation.

    Figure 1 -- Interface for type "String" (in file "String.h")
    #ifndef STRING_
    #define STRING_
    #include <iostream>
    using namespace std;
    class String
    // Capacity of a string
    static const int MAX = 64;
    // Construct empty string
    String() { Length = 0; }
    // Construct string using array of characters
    String( const char Source[] );
    // Return string length
    unsigned length() const { return Length; }
    // Return reference to element I
    char& operator[]( unsigned I ) { return Mem[I]; }
    const char& operator[]( unsigned I ) const { return Mem[I]; }
    // Append string Source to the current string
    String& operator+=( const String& Source );
    // I/O operations
    friend istream& operator>>( istream& Out, String& One );
    friend ostream& operator<<( ostream& Out, const String& One );
    char Mem[MAX]; // Memory to hold characters in string
    unsigned Length; // Number of characters in string
    // Return string which is the concatenation of strings One and Two
    String operator+( const String& One, const String& Two );
    // Compare two strings (equality operator)
    bool operator==( const String& One, const String& Two );

  5. #5
    Registered User
    Join Date
    Jan 2005
    Given the answer from the sample exam, the interface could be assumed. The one posted matches what would be expected.

    To see when and where a constructor is called, you must also look through the sample program and look at all potential function calls. This includes the operator>>, operator<<, and operator== functions.

    If a String is passed by reference to one of these functions, then no constructor is called because a pass by reference doesn't create a new object, it just passes a reference to an existing one.

    If a String is passed by value to one of these functions, then a copy is created using the copy constructor, and so a constructor is called. Also, if a String is returned by value, then again a copy is made, and a constructor is called (think about what constructor would be called if the sample program used operator+).

    Finally, if a function parameter takes a String reference, but something else is passed (like a character array), then a conversion must take place. A conversion from one type to the String type would use a constructor to create a temporary instance of the String type which could then be passed to the function that takes a String reference. This is where the last two constructor calls occur. See if you can see and understand where that is happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM