C++ references with user-defined classes

Started by
4 comments, last by cobra_niteviper 16 years ago
Hello, I have what I think is pretty well-defined code, but I'm having a conceptual problem somewhere. I have a variable which is passed in as a reference and is supposed to be modified, but when control returns to main, the variable is not modified. The variable in question is "history". Line::Process should modify history, which can then be printed out from main as history.Print(). I have a nesting of classes, including vectors, and I have heard that user-defined vectors need to have copy constructors and assignment defined, which I have done. I am having trouble debugging because I am newb. Thanks.
[SOURCE]
int main()
{
    // Variables
  Record history;   // Stores the input data
  Line inputLine(history);  // Stores lines from the sig file.

    // Body
  inputLine.ReadLine();   // Gets a new line  
  inputLine.Process();    // Uses line to modify history
//  }
  history.Print();        // Prints the history
  return 0;
}

Line::Line(Record& o_history)
{
  history = o_history;
  SetupInfile();
}

Record& Record::operator =(const Record& record2)
{
  allCatalysis = record2.allCatalysis;
  return *this;
}

class Record

{
 public:
  vector <Catalysis> allCatalysis;
 // and other things
};

Catalysis& Catalysis::operator= (const Catalysis& outer)
{
  bioChemicalReactionNum = outer.bioChemicalReactionNum;
  publicationXrefNum = outer.publicationXrefNum;
  sequenceParticipantNum = outer.sequenceParticipantNum;
  isWritten = outer.isWritten;
  return *this;
}
[/SOURCE]
Advertisement
Where is Line::history declared?

Also, on these forums it's [ source ] rather than .
Line::history is declared within class Line as:
Record history;
Since history is not itself a reference, all this does:
history = o_history;
Is copy the value of o_history to history.

What you probably want is:
Record& history;
This will cause the Line class to store a reference to the Record object that is passed in (rather than a copy of it). Any modifications made to this object within the Line class will then be reflected in the original object.

Note that once you make history a reference, you'll need to initialize it (using an initializer list) rather than assign it a value in the constructor.

As for creating your own copy constructor and assignment operator, based on what you've posted, it doesn't look like it's necessary. For details on the whys and wherefores, google 'c++ rule of three'.
Quote:Original post by jyk
What you probably want is:
Record& history;
This will cause the Line class to store a reference to the Record object that is passed in (rather than a copy of it). Any modifications made to this object within the Line class will then be reflected in the original object.

Note that once you make history a reference, you'll need to initialize it (using an initializer list) rather than assign it a value in the constructor.

Just illustrating the things that jyk said, in code:
class Line{...   Record& history;//the member variable must also be a reference!}...Line::Line(Record& o_history) : history(o_history)//references can only be initialized in initializer lists{  SetupInfile();}
Thank you very much, I did not know about initializer lists so I had no idea how to get a reference passed in there. Maybe I had seen it before but it is so hard to remember these things if you didn't think you'd ever need them! My code now runs with no errors and gives me my output. Thanks again.

This topic is closed to new replies.

Advertisement