Need help.

This is a discussion on Need help. within the C++ Programming forums, part of the General Programming Boards category; In a program, it has class simplecat { public: simplecat(); simplecat(simplecat&); ~simplecat(); .... .... etc... What does this offer, why ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    50

    Need help.

    In a program, it has
    class simplecat
    {
    public:
    simplecat();
    simplecat(simplecat&);
    ~simplecat();

    ....
    ....
    etc...
    What does this offer, why is the ampersand (&) by the word cat, and not simple.
    This program is a part of samspublishing 21 days.
    I don't understand this
    please help
    thanks.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    because simplecat(simplecat) would be an infinite loop. The function we are talking about there is called the copy constructor. It is called whenever a copy of an object needs to be made. If we pass a simplecat to it by value then we would have to copy it hence calling the copy constructor. To get around this and also for reasons of efficiency we pass the object by reference or better still by const reference. The ampersand denotes that this is a reference. simplecat& is reference to simplecat.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

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