简体   繁体   中英

STL Container of Custom Classes Doesn't Hold Member's Values

I am having trouble using a container with my custom class. Every time I try to make an object and push it, the member variables in the pushed object show up as zero. So, myConnection.value appears 0 instead of its value. I suspect there is something wrong with my copy constructor.

Global Definition

vector<connection> Connections;

The trouble spot

    while ( dataInput.good() )
{
    getline (dataInput,line);
    sscanf(line.c_str(),"%lg %lg %d %d",&k,&order,&final,&initial);
    printf("%lg %lg %d %d\n",k,order,final,initial);//Looks Good
    Connections.push_back(connection(k,order,final,initial));
    printf("%lg %lg %s %d\n",Connections[i].k,Connections[i].order,Connections[i].to,Connections[i].from);//Everything is zero!?
    i++;
}

Connection.cpp

#include "../include/connection.h"
#include <stdio.h>
connection::connection(double kin, double orderin, int fromin,int toin)
{
    k=kin;
    order=orderin;
    from = fromin;
    to=toin;
printf("%d %d %i %i\n",kin,orderin,fromin,toin);

}

connection::~connection()
{
    //dtor
}

connection::connection(const connection &c)
{
    connection(c.k,c.order,c.from,c.to);
}

Connection.h

#ifndef CONNECTION_H
#define CONNECTION_H


class connection
{
public:
    connection(double, double, int,int);
    connection(const connection& c);
    virtual ~connection();
    double k;
    double order;
    //From which mass to which mass
    int from;
    int to;
};

#endif // CONNECTION_H

Output

100 1 1 2
1 2 1 1
1 2 1 450294344
0 0 (null) 0
100 1 2 3
2 3 2 1
2 3 2 450294376
0 0 0 1
0 0 (null) 0
100 1 1 3
1 3 1 1
1 3 1 450294440
0 0 0 1
0 0 0 1
0 0 (null) 0

Your copy-constructor creates a temporary connection instance with the specified values and immediately destroys it, leaving the newly copy-constructed object completely uninitialized. A correct, idiomatic copy-constructor would be:

connection::connection(const connection& c)
  : k(c.k),
    order(c.order),
    from(c.from),
    to(c.to)
{ }

That said, it's worth noting that in this case you don't actually need a copy-constructor at all -- because you don't have any data member allocation/lifetime issues to worry about (read: no raw pointers), the compiler will generate an ideal one for you (the same goes for your destructor). So, the best fix here is simply to get rid of your copy-constructor (and destructor) altogether.


Unrelated to your real problem, note that you should prefer member-initializer-lists to assignment in constructors. So, this would be preferable to your current constructor:

connection::connection(double kin, double orderin, int fromin, int toin)
  : k(kin),
    order(orderin),
    from(fromin),
    to(toin)
{
    printf("%f %f %i %i\n", k, order, from, to);
}

(Also note that %d is an invalid format-specifier for double .)

You should almost never write a copy constructor yourself. If in doubt, don't do it. The cases when you will need to write one are:

  • when you have a non-empty destructor
  • when you want to prevent copying

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM