-
May 26th, 2013, 10:18 AM
#1
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.
-
May 26th, 2013, 10:36 AM
#2
Re: New data type in method declaration
That looks like it should work, though the typedef is very unnecessary. How does it not work?
-
May 26th, 2013, 10:50 AM
#3
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 []);
};
-
May 26th, 2013, 10:54 AM
#4
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.
Last edited by 2kaud; May 26th, 2013 at 11:00 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 26th, 2013, 11:00 AM
#5
Re: New data type in method declaration
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.
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?
-
May 26th, 2013, 11:04 AM
#6
Re: New data type in method declaration
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!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 26th, 2013, 11:07 AM
#7
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.
-
May 26th, 2013, 11:11 AM
#8
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?
-
May 26th, 2013, 11:13 AM
#9
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.
-
May 26th, 2013, 11:22 AM
#10
Re: New data type in method declaration
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.
-
May 26th, 2013, 11:35 AM
#11
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);
-
May 26th, 2013, 11:39 AM
#12
Re: New data type in method declaration
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]);
-
May 26th, 2013, 11:57 AM
#13
Re: New data type in method declaration
Thanks, you were absolutely right!
-
May 26th, 2013, 04:06 PM
#14
Re: New data type in method declaration
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.
Last edited by nuzzle; May 27th, 2013 at 03:34 AM.
-
May 26th, 2013, 04:22 PM
#15
Re: New data type in method declaration
Thank you for advice. I'll try to follow it.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|