Jump to content
  • Advertisement
Sign in to follow this  
Reegan

Inner struct/class

This topic is 3413 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

Hi everyone, im trying to define a struct inside a class (i know structs and classes are 99.0% identical, but for simplicties sake... ) I can do this with structs that do not contain const members but i need const members, and i also would like to initialise them without a constructor for the inner struct. Plus, the data members of the inner struct are all doubles. So what would be the best way to go about doing what is described. Thanks to those who push me closer to solving the problem described. ~Reegan

Share this post


Link to post
Share on other sites
Advertisement
if you make the variables static you can declare them inside the struct with no constructor

static const int an_integer = 10;

Share this post


Link to post
Share on other sites
Quote:
Original post by Stowelly
if you make the variables static you can declare them inside the struct with no constructor

static const int an_integer = 10;


Yeah, i tried, that but it doesnt work, i get the "only static const integral.." compiler error. perhaps this is because im using doubles? i dont know.

#ifndef CLASS_TOOLS_MATHMATICS_H
#define CLASS_TOOLS_MATHMATICS_H


/* DLL export/import */
#ifdef EXPORT_ENGINEUTILS_DLL
#define ENGINEUTILS_DLL __declspec(dllexport)
#else
#define ENGINEUTILS_DLL __declspec(dllimport)
#endif



#include <cmath>

namespace TOOLS
{
class ENGINEUTILS_DLL Mathmatics
{
public: // public methods
Mathmatics( void );


float Cos ( float theta );
float Sin ( float theta );
float Tan ( float theta );
float Acos ( float theta );
float Asin ( float theta );
float Atan ( float theta );
float DegToRad ( float degree );
float RadToDeg ( float radian );


public: // public data
struct {
static const long double Whole = 3.14159265358979323846;
static const long double Half = 1.57079632679489661923;
static const long double Quarter = 0.785398163397448309616;

operator double() const { return Whole; }
operator float() const { return (float)Whole; }
} Pi;
};
}



#endif /* CLASS_TOOLS_MATHEMATICS_H */




Work In Progress code

[Edited by - Reegan on March 12, 2009 4:26:05 PM]

Share this post


Link to post
Share on other sites
with statics you need to also define them outside the class like so:

(beware untested and possibly not correct)

//some code

public: // public data
struct {
static const long double Whole = 3.14159265358979323846;
static const long double Half = 1.57079632679489661923;
static const long double Quarter = 0.785398163397448309616;

operator double() const { return Whole; }
operator float() const { return (float)Whole; }
} Pi;
};
Mathmatics::Pi const long double Whole = 3.14159265358979323846;

}

Share this post


Link to post
Share on other sites
hmmm, yeah i thought i had to do that, but only when i dont initialise inside the class/struct itself.. however, this means it effectively becomes like a global in a namespace, which is kind of undesireable, but what choice do i have =/

Thanks for your help! Would appreciate it if you and others would give more suggestions on what to do here.

In order to the suggestion you posted, id have to give the Pi struct a name because at the moment it is unnamed, and so not allowing me to write

const long double Mathmatics::Pi::Whole = 3.14...;

Share this post


Link to post
Share on other sites
hmm ok how about

public: // public data
struct {
#define Whole 3.14159265358979323846
#define Half 1.57079632679489661923
#define Quarter 0.785398163397448309616

operator double() const { return static_cast<long double>(Whole); }
operator float() const { return static_cast<float>(Whole); }
} Pi;

Share this post


Link to post
Share on other sites
As far as I know, #define's don't honor scope the way you'd think in that code snippet. The preprocessor runs over the # commands and replacements before the compiler ever touches the code. I could be wrong, because I've never seen anything done like that... but that in itself probably says something.

Reegan: Well, why not use an enum? Sounds like exactly what you want: (NOTE: Only works with ints, and I didn't realize you were using doubles till after I posted... and then there's the odd PI thing)


class myOuter
{
enum myEnum { fooIsZero, fooIsOne, fooIsTwenty = 20, fooIsTwentyOne };
};


You can't modify the values in an enum, and you don't need to - can't actually - create an instance of an enum.

~Jonathan


EDIT: (with above note) Why are you storing Half and Quarter values for PI? Not to mention, you're using conversion operators (you should have a very good reason) when a simple cast would do fine. :|

Share this post


Link to post
Share on other sites
Quote:
Original post by Twisol
As far as I know, #define's don't honor scope the way you'd think in that code snippet. The preprocessor runs over the # commands and replacements before the compiler ever touches the code. I could be wrong, because I've never seen anything done like that... but that in itself probably says something.

Reegan: Well, why not use an enum? Sounds like exactly what you want: (NOTE: Only works with ints, and I didn't realize you were using doubles till after I posted)


class myOuter
{
enum myEnum { fooIsZero, fooIsOne, fooIsTwenty = 20, fooIsTwentyOne };
};


You can't modify the values in an enum, and you don't need to - can't actually - create an instance of an enum.

~Jonathan


EDIT: (with above note) Why are you storing Half and Quarter values for PI? Not to mention, you're using conversion operators (you should have a very good reason) when a simple cast would do fine. :|


enums can onyl hold integer values, im looking for doubles ;)
Im using them because in math.h they are considered to be common math constants so i placed them there for good measure, i have the casting operators there so i dont have to Write Math.Pi.Whole all the time and i can just write Math.Pi instead. When Math.Pi is confronted by a float or double it will return the value of Pi instead.

Share this post


Link to post
Share on other sites
I realized that! See edit and edited-in note right above the enum code. [smile]

Hmm... let me think on this. I still don't get why you want to do this, though.

Share this post


Link to post
Share on other sites
My reason for doing this is for encapsulation (as oppose to global constants or defines), readabilty and my logical way of thinking ( Pi is a part of math so it should be encapsulated in the scope of mathmatics )

I dont expect anyone to agree, its just how i want to do things, i dont expect anyone else to use it. So i can see no harm, but good there.

If you come up with something please share, thanks [smile]

When im finished, i should be able to do something like this:

Player.Position.x += Math.Cos( Math.DegToRad( Player.Direction ) ) * Player.Speed
Player.Position.y -= Math.Sin( Math.DegToRad( Player.Direction ) ) * Player.Speed


Obviously Pi will be used elsewhere, but the above is what im aiming for.

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!