New data type in method declaration
Hello, I want my method of the class to take as an input an array of data type derived from class. How can I do this?
Code:
class Planet {
public:
// typedef class Planet PlanetType;
void GetForce(int, PlanetType []);
};
Or when should I define my new data type PlanetType?
Thanks in advance.
Re: New data type in method declaration
That looks like it should work, though the typedef is very unnecessary. How does it not work?
Re: New data type in method declaration
It was complaining that PlanetType was not declared. I found the answer on other forum:
Code:
class Planet {
public:
// typedef class Planet PlanetType;
template<class PlanetType> void GetForce(int, PlanetType []);
};
Re: New data type in method declaration
Before the definition of class Planet, you will need to forward declare class PlanetType. The definition of class PlanetType then follows the definition of class Planet.
As an example
Code:
class PlanetType;
class Planet {
public:
void GetForce(int, PlanetType []);
};
class PlanetType : public Planet {
public:
void cPlaTyp();
};
where class PlanetType is derived from Planet and referenced in the base class definition Planet.
Re: New data type in method declaration
Quote:
Originally Posted by ted_kingdom
It was complaining that PlanetType was not declared.
Well yeah, but your code snippet was incomplete, plus you commented out the typedef. It would have been better if you had actually posted a more complete example of what you were trying to do, along with the error messages.
Quote:
Originally Posted by ted_kingdom
I found the answer on other forum:
Do you know what that does, or did you just accept it because the compiler stopped complaining?
Re: New data type in method declaration
Quote:
Originally Posted by
ted_kingdom
It was complaining that PlanetType was not declared. I found the answer on other forum:
Code:
class Planet {
public:
// typedef class Planet PlanetType;
template<class PlanetType> void GetForce(int, PlanetType []);
};
Not convinced this will do what you want - apart from silencing the compiler!
Re: New data type in method declaration
I commented it out just to let everybody know what was my intention. Unfortunately, I don't know what it does (I asked it, but still waiting for the response as my googling didn't answered it). If you know, please tell.
Re: New data type in method declaration
You would need to learn about template programming to understand what it does. You will probably learn this sooner or later, but for now, I suggest that you elaborate on what you mean by "method of the class to take as an input an array of data type derived from class". Are you talking about inheritance, as in 2kaud's example? If so, why does the base class need to know about a particular derived class in its interface?
Re: New data type in method declaration
Actually, you are right. Compiler doesn't complain, but I don't get the output as I expected.
So, in my program I need to use a method of the class that takes as an input an array of data type derived from the very same class. The reason for that is that I need a function that can access private members of class, and can work with an array, again derived from class. Hope my explanation is not too confusing. I can go into more details, but it, probably, would be irrelevant.
Re: New data type in method declaration
Quote:
Originally Posted by ted_kingdom
use a method of the class that takes as an input an array of data type derived from the very same class
What do you mean by "array of data type derived from the very same class"? For example, suppose the class is named X. Is this variable named xs an "array of data type derived from" X?
If not, show an example.
Re: New data type in method declaration
So, I have a class Planet:
Code:
class Planet {
public:
void Verlet(int, double []); // a method to implement Verlet algorithm
void VelocityVerlet(int, double []);
void LeapFrog(int, double []);
void EulerForward(int, double []);
void GetForce(int, PlanetType []);
private:
double rX_current, rY_current, rZ_current; // current position of planet
double rX_previous, rY_previous, rZ_previous; // previous position of planet
double vX, vY, vZ; // velocity
double m; // mass
double ForceX;
double ForceY;
double ForceZ;
};
Now, I want to define a new data type on its basis:
Code:
typedef class Planet PlanetType;
I create an object of class, e.g. newPlanet.
Then I create an array as follows:
Code:
PlanetType *SolarSystem;
SolarSystem = new PlanetType [10];
And finally, I want to call a method that would take my array 'SolarSystem' as input:
newPlanet.GetForce(N, SolarSystem);
Re: New data type in method declaration
Quote:
Originally Posted by ted_kingdom
Now, I want to define a new data type on its basis:
Code:
typedef class Planet PlanetType;
But this is unnecessary: why use a typedef when you can just use the Planet type?
You can just write:
Code:
class Planet {
public:
void Verlet(int, double []); // a method to implement Verlet algorithm
void VelocityVerlet(int, double []);
void LeapFrog(int, double []);
void EulerForward(int, double []);
void GetForce(int, Planet []);
private:
double rX_current, rY_current, rZ_current; // current position of planet
double rX_previous, rY_previous, rZ_previous; // previous position of planet
double vX, vY, vZ; // velocity
double m; // mass
double ForceX;
double ForceY;
double ForceZ;
};
// ...
Planet* SolarSystem = new Planet[N];
newPlanet.GetForce(N, SolarSystem);
Note that instead of using new Planet[N], it would be advisable to use a container, e.g.,
Code:
std::vector<Planet> SolarSystem(N);
newPlanet.GetForce(N, &SolarSystem[0]);
Re: New data type in method declaration
Thanks, you were absolutely right!
Re: New data type in method declaration
Quote:
Originally Posted by
ted_kingdom
my array 'SolarSystem' as input:
A good rule of thumb is to make each class represent a single concept. So I suggest you limit Planet to represent one celestial body only, and introduce a new SolarSystem class to represent many Planets.
There's no reason why Planet should know about and expose numerical methods of how to integrate Newton's equations of motion such as Verlet. It's not planet related information so it's much better to pass it in when needed to calculate the new position and velocity given the total force tugging on the planet.
And there's no reason why Planet should be able to do things involving many Planets, such as calculating forces induced by the influence of other Planets. One of something and many of the same thing are different entities and are better modeled separately. For example a litter or a pack or a kennel are not the same as a dog and shouldn't be modeled by a Dog class. So calculations involving many Planets belong in SolarSystem, not Planet.
Furthermore you shouldn't expose how Planets are stored, be it in an array or a vector or something else. SolarSystem holds many Planets but there's no reason why anyone on the outside must know exactly how. Better keep that an implementation secret so it can be changed independently.
My advice is that you rethink your design. One concept per class only and no unnecessary exposure of implementation detail. Then everything becomes less intertwined and the C++ coding more straightforward.
Re: New data type in method declaration
Thank you for advice. I'll try to follow it.
Re: New data type in method declaration
Quote:
Originally Posted by
ted_kingdom
And finally, I want to call a method that would take my array 'SolarSystem' as input:
Another thing you should realize is that none of your functions actually are passed arrays.
For example, this definition:
Code:
void Verlet(int, double []); // a method to implement Verlet algorithm
Is no different than this one:
Code:
void Verlet(int, double*); // a method to implement Verlet algorithm
When you pass an array, you aren't passing an array -- you are passing a pointer to the first element of the array. So what ends up happening is that the array decays to a mere pointer on the receiving function.
Regards,
Paul McKenzie