Thread: clone() function, what do I need it for?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    clone() function, what do I need it for?

    Hi.
    I'm studying polymorphism & inheritance and recently came across this clone() function that got me really confused.
    what is this clone() function, and when do I need to worry about it?
    thanks in advanced!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When you need to copy an object of a derived class, but you are only working with (smart) pointers to the base class and shouldn't have to know that the object is of which derived class, then you'll have to worry about it
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    OK, great.

    and copy constructors works as usual? I mean, derived class also inherits the Base's copy contructor, right?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Absurd
    I mean, derived class also inherits the Base's copy contructor, right?
    Not quite: the derived class copy constructor would not accept a base class object as an argument to copy from.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    228
    so what if I have a Base class with dynamic allocated memory?
    do I need to explicitly call the Base's copy constructor from the derived class' copy constructor?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Absurd
    so what if I have a Base class with dynamic allocated memory?
    do I need to explicitly call the Base's copy constructor from the derived class' copy constructor?
    If you don't explicitly define the copy constructor of the derived class, then no, since the compiler generated one should do the right thing. Otherwise, you do (in the initialiser list), if that is the right thing to do (and it probably is).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    228
    thanks for your help, laserlight.

    Quote Originally Posted by laserlight View Post
    If you don't explicitly define the copy constructor of the derived class, then no, since the compiler generated one should do the right thing. Otherwise, you do (in the initialiser list), if that is the right thing to do (and it probably is).
    but don't I have to explicitly define the copy constructor of the derived class?
    otherwise I'll get stuck with a shallow copy of the Base's data members...
    or maybe that's what you meant by "if that is the right thing to do"...?
    sorry for the ignorance, this is all new to me.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you don't have to, unless the derived class itself would be doing some manual resource management, but in that case, I think it would be better to separate out their components into classes that manage their own resources.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    228
    OK, consider the following code:

    Code:
    class A
    {
    protected:
        /* some pointers that points to
         * dynamic allocated memory here*/
    public:
        A(){/*constructor*/}
        virtual ~A(){/*destructor*/}
        A(const A& a){/*some copy is going on right here*/}
        /* maybe some more methods here*/
    };
    
    
    class B: public A
    {
        /*some more functionality here, no CCtr here*/
    };
    
    
    void func(B obj)
    {
        /*some function*/
    }
    
    
    int main()
    {
        B bObject;
        //sending bObject by value will trigger A's CCtr?
        func(bObject);
    }
    would you say that a CCtr is necessary here?


    Quote Originally Posted by laserlight View Post
    No, you don't have to, unless the derived class itself would be doing some manual resource management, but in that case, I think it would be better to separate out their components into classes that manage their own resources.
    what does "manual resource management" mean?


    thanks again for the help.
    appreciate it.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Absurd
    /* some pointers that points to
    * dynamic allocated memory here*/
    Not a good idea. Where feasible, you should be using the standard (or other) containers or smart pointers rather than such pointers with ownership. If you do need to have such a pointer for some reason, only have one per class. More than that and it usually becomes tricky to manage.

    Quote Originally Posted by Absurd
    would you say that a CCtr is necessary here?
    Probably not.

    Quote Originally Posted by Absurd
    sending bObject by value will trigger A's CCtr?
    No, it will invoke B's copy constructor (which is more normally abbreviated as "copy ctor"). This copy constructor will then invoke the A sub-object's copy constructor.

    Quote Originally Posted by Absurd
    what does "manual resource management" mean?
    As a rule of thumb, if the resource is memory, it means that somewhere in your code, you are calling delete or delete[] to match some new or new[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clone problems
    By The-Forgotten in forum Linux Programming
    Replies: 16
    Last Post: 12-10-2011, 12:18 PM
  2. A C Function clone of PHP's file get contents()
    By dtmuk in forum C Programming
    Replies: 6
    Last Post: 05-21-2009, 05:06 PM
  3. is it possible to pass arguments to the clone fn function?
    By smoking81 in forum Linux Programming
    Replies: 4
    Last Post: 09-12-2008, 10:27 AM
  4. Clone function
    By gustavosserra in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2004, 05:57 PM