Jump to content
  • Advertisement
Sign in to follow this  
cobra_niteviper

C++ references with user-defined classes

This topic is 3854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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'.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!