0

I am debugging some simulation software that has been written in C++, and am having trouble understanding part of the code.

The software simulates some entities moving in a 3D world, and their behaviour can be set such that they move in a straight line from A to B, spiral around a particular point, or follow a predefined route, etc.

As an entity moves in the simulation, some information about its movements is presented to the user- such as: its current speed, heading (direction), the distance to its current target, the time it will take to reach its current target & ETA at that location, etc.

The bug that I am currently working on is that the ETA and 'time to finish current leg' of a journey are displaying incorrect values to the user.

I have come across the following code in the C++ source file that is displaying this information to the user:

DateTimeAndZone fpETA(mCurrentTime);
SystemTimeTypes::addRelativeTimeToSystemTime(fpETA, flightPlanETA);

DateTimeAndZone is a class, so fpETA is an instance of that class... What I don't understand is how mCurrentTime (which is also a DateTimeAndZone), can be passed as a parameter of the instance of a class- in what appears to be the same way that a parameter is passed to a function.

If I go to the defintion of the DateTimeAndZone class, there is a return statement at the end of it that returns a static std::string_classname.

Can anyone explain to me how a class can be passed as the parameter for the creation of a new class? What are the implications of this? Does it mean that the new class will take on the information stored in the existing class? Or is there something that I am missing here?

I came across this question on SO, but it doesn't really answer my question... Passing a class object as an argument in C++

Community
  • 1
  • 1
Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

4 Answers4

1

DateTimeAndZone fpETA(mCurrentTime);

This calls the constructor of DateTimeAndZone, with the parameter defined. If mCurrentTime is a DateTimeAndZone object, by default the copy constructor will be called, or the copy constructor of DateTimeAndZone if it has been defined.

Gerald
  • 690
  • 4
  • 13
0

This is called the copy constructor. It creates a new DateTimeAndZone object by copying the data across from the mCurrentTime object. The signature of such a function is usually:

DateTimeAndZone (const &DateTimeAndZone);

If you look in the class definition for a method with that signature, you should see what this particular copy-constructor does.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

A class can be passed as a parameter just like any other type, you really need to be looking at the implementation (if there is no documentation) of that constructor to see what it uses the parameter for.

In general use this type of constructor is called a copy-constructor, there are lots of questions explaining the use of copy constructors and also the implications that come with them:

When do we have to use copy constructors?

The copy constructor and assignment operator

What is a copy constructor in C++?

What is the difference between a deep copy and a shallow copy?

Community
  • 1
  • 1
abcthomas
  • 131
  • 7
0

The following :

class DateTimeAndZone;
DateTimeAndZone fpETA(mCurrentTime);
// ^ this invokes:
DateTimeAndZone::DateTimeAndZone(const DateTimeAndZone &arg);

The last line is the copy constructor of the DateTimeAndZone class. This copy constructor will create a new DateTimeAndZone object that will hold a copy of the argument you passed (in that case mCurrentTime).

The copy constructor can be automatically generated by the compiler (you can study this slideshare to see in which cases it is or it is not) or user defined.

Rerito
  • 5,886
  • 21
  • 47
  • 'mCurrentTime (which is also a DateTimeAndZone)'. He clearly states it is an object of type 'DateTimeAndZone'. – abcthomas Jan 28 '15 at 10:53