Like Tree1Likes
  • 1 Post By laserlight

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

This is a discussion on clone() function, what do I need it for? within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm studying polymorphism & inheritance and recently came across this clone() function that got me really confused. what is ...

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

    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
    21,415
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    67
    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
    21,415
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    67
    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
    21,415
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    67
    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
    21,415
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2013
    Posts
    67
    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
    21,415
    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[].
    King Mir likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:18 AM
  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, 04:57 PM

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