CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2013
    Posts
    16

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: New data type in method declaration

    That looks like it should work, though the typedef is very unnecessary. How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2013
    Posts
    16

    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 []);
    };

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: New data type in method declaration

    Quote Originally Posted by ted_kingdom View Post
    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)

  7. #7
    Join Date
    Apr 2013
    Posts
    16

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2013
    Posts
    16

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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?
    Code:
    X xs[100];
    If not, show an example.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Apr 2013
    Posts
    16

    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);

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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]);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Apr 2013
    Posts
    16

    Re: New data type in method declaration

    Thanks, you were absolutely right!

  14. #14
    Join Date
    May 2009
    Posts
    2,413

    Re: New data type in method declaration

    Quote Originally Posted by ted_kingdom View Post
    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.

  15. #15
    Join Date
    Apr 2013
    Posts
    16

    Re: New data type in method declaration

    Thank you for advice. I'll try to follow it.

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured