V.quick question 'object&'

This is a discussion on V.quick question 'object&' within the C++ Programming forums, part of the General Programming Boards category; Hello, Very quick question. What does this mean when it is a constructor? Rectangle(const Rectangle&); What does this type of ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    24

    V.quick question 'object&'

    Hello,

    Very quick question.

    What does this mean when it is a constructor?

    Rectangle(const Rectangle&);

    What does this type of constructor receive?

    Your help and/or advice is greatly appreciated.

    Regards, global

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    A constructor is what the compiler tells an object to do when its created.

    Example:
    Code:
    class foo {
    public:
      foo(int parameter) {
        bar = parameter;
      }
    
    private:
      int bar;
    }
    In the above example when you call something like:

    Example:
    Code:
    foo x(5);
    It will create a brand new foo object where the bar member is set to 5. Now the one you said is called a "copy constructor" since it can be used to copy an object over. It will allow you to initialize one object by setting it equal to another object of the same type upon object creation. For doing the same at any other point you would need to look into overloading the equals operator.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    are you sure it doesn't say
    Code:
    Rectangle(Rectangle const &rect);
    or something similar?
    That's the standard copy constructor declaration.

    The constructor takes a reference to and object of the same class, usually to instantiate an identical copy of that object.

    Otherwise I've no idea since the stub you give doesn't give a type for Rectangle and the & operator gores before the variable name not after.

    (damn beaten to it)

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    Rectangle(const Rectangle&);
    ^ prototype my good friend. Nothing illegal or erroneous that I see.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by master5001
    Code:
    Rectangle(const Rectangle&);
    ^ prototype my good friend. Nothing illegal or erroneous that I see.
    doh

    that's 2 stupid mistakes in less than 2 minutes.
    I should go to bed now. (I hate when people don't declare the variable name in the prototype, I hate it, hate it, hate it)

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I hear ya. And no biggy, we are all allowed to be wrong. Another advantage to using names in a prototype (aside from the fact that my compiler will tell you them by name) is that they can clue someone reading over the header as to what the parameter does.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    master5001, sigfriedmcwild - thank you very much for your help.

    I did not know that if it has:

    Rectangle(const Rectangle&);

    ...that it signified a copy constructor - but i suppose (thinking about it), it must do because your using one object in the creation of another. So, if i wanted to do this somewhere else in the program, i would have to overload the '=' operator.

    I understand the general concept of it, however (because i'm new-ish to this) - i am slightly confused as to why there needs to be an '&' operator after the object - why can't i just have the object as the parameter?

    I really do appreciate your time and help.

    Thank you once again.

    Regards, gloabl

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The copy constructor isn't as intuitive as it seems once you understand how to use it. I mean to anyone starting just using the equals operator should be sufficient. But once you realize that an operator can only be called after an object is created, you realize "oh wait! it makes perfect sense now!"

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    The & indicates that you are passing a reference instead of a copy of the object.
    Copying objects is bad cause: 1) they are big, 2) unless you do it properly they don't get copied right (after all that's why you are implementing a copy constructor, if copying the object was trivial you wouldn't bother)

    For what a reference is exactly... well I'll let someone else more versed with words than me do this part. If I tried I'm sure it would come out as something completely wrong.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >For what a reference is exactly
    you do fine.

    Its a reference to another object. It isn't really its own object, its just a placeholder for another object at a given place in memory. It parallels the way pointers work.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    24
    Thank you for your explanation - i knew that the '&' operator passed something by reference, it's just that, i guess when your starting - your never quite sure whether or not it could mean something completely different - so in this case, it's just being passed by reference, to make sure you actually receive the real object. I get it now.

    Thank you for your clear and quick help, it is very much appreciated.

    Regards, global

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    First, a quick review of references:

    The '&' means that the parameter is a reference to an existing object. When you don't have the '&', then a copy of the argument passed to the function is made so that you aren't actually changing the original variable. For example:
    Code:
    #include <iostream>
    
    void foo(int intCopy)
    {
        intCopy = 5;
    }
    
    void bar(int& intReference)
    {
        intReference = 7;
    }
    
    int main()
    {
        int mainInt = 3;
        foo(mainInt);
        std::cout << mainInt << std::endl; // outputs 3.
        bar(mainInt);
        std::cout << mainInt << std::endl; // outputs 7.
    }
    The bar function changes the value of mainInt because it takes a reference to it as its parameter. The foo function doesn't change mainInt because it gets a copy of the value of mainInt.

    When passing class objects by value (not by reference), the compiler makes a copy of the object to give to the function. The way you make a copy of a class instance is to call the copy constructor.


    So you can see how maybe using an object parameter instead of a reference might not work for copy constructors. If it took an object by value, then in order for the copy constructor to run it would have to call the copy constructor to make a copy, which would have to call the copy constructor to make a copy, which would have to call the copy constructor to make a copy, and so on and so forth. So a reference is used so that you can get the data from an object without making a copy.

    [EDIT] - Beaten severely, but the key is that you cannot take an object as a parameter for the copy constructor because that would require a copy that requires the copy constructor.
    Last edited by jlou; 12-16-2004 at 03:58 PM.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >Beaten severely, but the key is that you cannot take an object as a parameter for the copy constructor because that would require a copy that requires the copy constructor.

    True, but what you lacked in speed you more than made up for in a quality thorough explanation with inclusive examples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    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 Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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