Constructors

This is a discussion on Constructors within the C++ Programming forums, part of the General Programming Boards category; //Listing 9.10 // Passing pointers to objects #include <iostream> using namespace std; class SimpleCat { public: SimpleCat (); // constructor ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Constructors

    //Listing 9.10
    // Passing pointers to objects

    #include <iostream>

    using namespace std;
    class SimpleCat
    {
    public:
    SimpleCat (); // constructor
    SimpleCat(SimpleCat&); /* what does this symbol & to the right of a parameter mean like here in SimpleCat&?*/
    ~SimpleCat(); // destructor
    };

    SimpleCat::SimpleCat()
    {
    cout << "Simple Cat Constructor...\n";
    }

    SimpleCat::SimpleCat(SimpleCat&)
    {
    cout << "Simple Cat Copy Constructor...\n";
    }

    SimpleCat::~SimpleCat()
    {
    cout << "Simple Cat Destructor...\n";
    }

    SimpleCat FunctionOne (SimpleCat theCat);
    SimpleCat* FunctionTwo (SimpleCat *theCat);

    int main()
    {
    cout << "Making a cat...\n";
    SimpleCat Frisky;
    cout << "Calling FunctionOne...\n";
    FunctionOne(Frisky);
    cout << "Calling FunctionTwo...\n";
    FunctionTwo(&Frisky);
    return 0;
    }

    // FunctionOne, passes by value
    SimpleCat FunctionOne(SimpleCat theCat)
    {
    cout << "Function One. Returning...\n";
    return theCat;
    }

    // functionTwo, passes by reference
    SimpleCat* FunctionTwo (SimpleCat *theCat)
    {
    cout << "Function Two. Returning...\n";
    return theCat;
    }
    Last edited by incognito; 12-30-2001 at 09:05 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    >>SimpleCat(SimpleCat&);

    Means that SimpleCat can recieve a reference to another SimpleCat in its constructor.....

    The '&' means it is passed by reference and not copy, so when the function call is made to that constructor, the overhead of copying an entire copy of a SimpleCat is avoided. On the downside, the value passed can be altered by that function which can lead to problems. Often the keyword 'const' would be used in the declaration to avoid this.
    Last edited by Fordy; 12-30-2001 at 08:56 AM.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    ok, now I am confused between this &SimpleCat and this SimpleCat&...please elaborate more.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    I'll try.....

    SimpleCat&

    This is used here as part of a function declaration. It means that the function expects to recieve an object of type "SimpleCat" passed by reference.

    SimpleCat(SimpleCat&);

    could be written as

    SimpleCat(SimpleCat& myCat);
    or
    SimpleCat(SimpleCat &myCat);

    The fact that you dont explictly need to give a name to the object/variable recived in the constructor declaration often makes it confusing.


    &SimpleCat

    You can get the memory address of the variable by putting '&' on the left of a variable.
    Often, you wouldnt use this like that because you would be trying to access the address of a class type......when you would more commonly want the address of the object that you had created an instance of. For example

    SimpleCat myCat; //an instance of a SimpleCat
    SimpleCat *ptrCat; //a pointer to an object of type 'SimpleCat'

    ptrCat = &myCat; //set the memory address of the pointer ptrCat to that of myCat

    Hope this helps

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    This is used here as part of a function declaration. It means that the function expects to recieve an object of type "SimpleCat" passed by reference.

    Which Function? do you mean FunctionOne(Frisky)? and if so, how is in FunctionOne(Frisky)...how is Frisky here passed by referenced?( What tells me that Frisky is function one is boing to be passed by reference) Because copy constructor is only called when FunctionOne(Frisky) is called.



    PS please help..................... this book is starting to confuse me.
    Last edited by incognito; 12-30-2001 at 11:05 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Ok let me try explaining this.
    When you pass a variable to a function normally such as in your FunctionOne, a copy of the object is made. The function can do what ever it wants with that variable, change it, leave it alone, it will remain the same in the calling function.
    If you pass by reference using FunctionName( type & varname), then the function can change the value of the variable, and the in the calling function the value will be changed.
    Passing by reference is like passing a pointer to a function, but is easier to use, becuase you dont have to do the pointer syntax (* derefencering and the -> operator when using pointers to classes) to change the variable.
    When a class object is passed normally, a copy of it needs to be made. The defualt copy constructor simply copys all the data from one object to the other, using the = operator. This is fine if the class only uses int's, char's, or other simple built in data types. If your class is using pointers to allocate memory using new, then the address of the copys pointer will point to the same memory as the originals pointer. THIS IS NOT GOOD, because the copy will use the same memory as the original.
    Hope that helps you
    A copy constructor is needed so you can call new and allocate new memory for the copy.
    All this takes alot of time, and can be avoided by passing the class by reference, but using the keyword const to keep the value the same
    void FunctionName( const ClassName & var);
    This will not make a copy of var, but the const keyword keeps the value from getting changed. So there is no overhead wasted making a copy of the var.
    When passing class objects you can always use reference & to be able to change the var, or const and & to keep it the same.

    A copy constructor is not always needed for your functions, but it is a very good idea to make one, in case someone else uses that classs, and tries to pass a copy, or if one of the standerd library functions is expecting to be able to pass a copy to another function.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok thanks for all your help but the main problem here is that this books gets ahead of itself at times...I DID NOT KNOW THAT EVERYTIME YOU PASS AN OBJECT VALUE TO A FUNCTION OR AS A FUNCTION RETURN VALUE a copy constructor is called automatically..........after looking at the index for copy constructor and finding the page number and looking at the topic I think I understand why SimpleCat(SimpleCat&) is the copy constructor and why it is called. Although I find this book to be pretty good and detailed at explaining things I am afraid that it gets ahead of itself at time.................
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21