Copy two objects in c ++


I am developing a script which consists of a library that stores books (which are the objects of the code). I would like to create a function called Book , its main function will be to copy two books, but I can not do it. The function is Libro::copiarLibro() , appears at the end of the file named Book.h:

I leave the code: Book.h:

#include <iostream>
#include <sstream>
#ifndef LIBRO_H_
#define LIBRO_H_

using namespace std;

class Libro {

    Libro (string t, string au, string gen, int num);
    Libro(string lineafich);

    virtual ~Libro();

    string  generarTejuelo();
    void setTejuelo();

    string getTitulo();
    void setTitulo(string titulo);

    string getAutor();
    void setAutor(string autor);

    string getGenero();
    void setGenero(string genero);

    int getNumPaginas();
    void setNumPaginas(int numPaginas);

    string aCadena();
    string formatoFichero();

    bool tieneDatos();
    Libro copiarLibro();

    string titulo;
    string autor;
    string  genero;
    string tejuelo;
    int numPaginas;


Libro Libro:: copiarLibro(){
    Libro l1, l2;
    l2 = l1;
    return l2;
asked by DDN 29.06.2016 в 19:36

2 answers


You have the function CopiarLibro() within public functions, which "binds" the current instance of class Libro to copy only that book.

To make it more global, you can do the following:

Libro* copiarLibro(Libro *from)
    Libro *rtn = new Libro(from->titulo, from->autor, /* otros parametros... */);
    return rtn;

In your code, what you do basically is to define 2 new books, whose value is not yet defined, and you assign a reference NULL of l2 to l1 , and does not return anything.

As I put it, you define 2 book variables, I used the following class for tests:

class Libro
        string Nombre;
Libro::Libro(string s)
    Nombre = s;

And the function that I have put you above to copy it:

Libro* copiarLibro(Libro *from)
    Libro *rtn = new Libro(from->Nombre);
    return rtn;

This is the main method:

int main()
    Libro *L1 = new Libro("Historias Chinas del 91");
    cout << "Nombre actual de L1: " << L1->Nombre << "\n"; // Imprime: Historias Chinas del 91.
    Libro *L2 = copiarLibro(L1);
    L1->Nombre = "Cuentos Nacionales"; // Le asigno el valor de otro nombre a L1.
    cout << "Nombre de L1: " << L1->Nombre << "\n";
    cout << "Nombre de L2: " << L2->Nombre << "\n"; 

    return 0;

Showing the following as a result:

Nombre Actual de L1: Historias Chinas del 91
Nombre de L1: Cuentos Nacionales
Nombre de L2: Historias Chinas del 91

Basically, you are going to copy an instance of a book A in another book B , to have the same book in 2 different variables.

EDIT: I have not understood exactly what the CopiarLibro() function is, but since your comment mentions that the function can not have parameters, then I think I understand the function's meaning inside the class.

In that case, use the same function as your class Libro and add the following to the prototype:

    Libro *CopiarLibro(); // El prototipo.

And in its definition:

Libro *Libro::CopiarLibro()
    Libro *L = new Libro(this->Nombre /* , otros parámetros. */);
    return L;

And let's try a new method main() :

int main() 
    Libro *ViejoLibro = new Libro("La biblia");
    cout << "El viejo libro: " << ViejoLibro->Nombre << "\n\n";

    Libro *NuevoLibro = ViejoLibro->CopiarLibro();
    cout << "El nuevo libro:" << NuevoLibro->Nombre << "\n";

    ViejoLibro->Nombre = "Vacio"; // Asignamos "vacio" como nombre del libro.
    cout << "El viejo libro: " << ViejoLibro->Nombre << "\n";
    return 0;

Giving the following result:

El viejo libro: La biblia

El nuevo libro:La biblia
El viejo libro: Vacio

I hope it has helped you!

answered by 29.06.2016 / 20:24

Well, to start your copy book function, the only thing you're doing is creating an instance of the empty book class and then copying its contents to another empty instance, which gives you an empty instance as return.

To achieve your goal you can do it in different ways here I will leave you 2 example:

1.- Function within the class


class Libro
   ...//constructor, destructor, variables y metodos
   void copiarLibro(const Libro& copia);


void Libro::copiarLibro(const Libro& copia){
   *this = copia;

2.- External function


void copiarLibro(const Libro& from,Libro& to);


void copiarLibro(const Libro& from, Libro& to){
   to = from;

Finally, do not forget that you could also simply copy the contents directly from your main function.

int main(int argc, char** argv){

   Libro A("Cuento de Terror","Autor","Terror",240)
   Libro B;
   B = A;

   //o bien con el método miembro

   return 0;

I hope you find it useful:)

answered by 29.06.2016 в 23:46