Sign in to follow this  

Inner struct/class

This topic is 3199 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
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
I don't see a problem with just doing this:


class Math
{
// .. other stuff
public:
static const double PI;

// .. other stuff
};

// static initialization
const double Math::PI = 3.14;


PI is still local to Math, not in the global namespace. And you just use it as "Math::PI".

~Jonathan

Share this post


Link to post
Share on other sites
Ah well thats just personal preferance, i prefer the dot operator to the scope resolution operator.

At the moment ive defined a PI struct outside the mathmatics class with a constructor that initialises everything in the initialiser list. Not exactly my first choice but hey, im not sure if i can do it the way i want to.


Am i really that different to other programmers (in a bad way)? Seems like, every piece of code i post, it seems to puzzle people lol.

Share this post


Link to post
Share on other sites
Quote:
Original post by Reegan
Ah well thats just personal preferance, i prefer the dot operator to the scope resolution operator.

Use C# or Java, then. In the long run that's a terrible reason to go out of your way for. =/

Quote:
Original post by Reegan
At the moment ive defined a PI struct outside the mathmatics class with a constructor that initialises everything in the initialiser list. Not exactly my first choice but hey, im not sure if i can do it the way i want to.


Well, at least you have it in a namespace, but that's going out of your way for something that doesn't help you in the long run. Is the :: operator that ugly to you? The makers of PHP liked it enough to gave it its own name - Paamayim Nekudotayim! [grin]

~Jonathan

Share this post


Link to post
Share on other sites
I do plan on using C# (allthough i do think C# uses an excessive amount of keywords which is kinda ugly) at some point, but the glamour of C++ and its infamous status won me over lol

Like i said, i dont expect people to understand my coding style, if it is wrong i think its just best to let me make my own mistakes, dont you? It means ill actually be learning rather than just taking peoples word for it. however that doesnt mean i dont want help from you or any other programmer at these forums.

PS: We seem to talk alot you and i on here, perhaps i should take your IM email address eh? lol. By the way, are you working on or have finished any games recently? i feel like playing something. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Reegan
I do plan on using C# (allthough i do think C# uses an excessive amount of keywords which is kinda ugly) at some point, but the glamour of C++ and its infamous status won me over lol

Fair enough. It's all in how you use the language, though. I actually find C# to be more well designed than C++. We're each entitled to our opinions [smile]

Quote:
Original post by Reegan
Like i said, i dont expect people to understand my coding style, if it is wrong i think its just best to let me make my own mistakes, dont you? It means ill actually be learning rather than just taking peoples word for it. however that doesnt mean i dont want help from you or any other programmer at these forums.

True, but there's a reason we generally tell people to use C++ rather than C: bad habits are often carried over, and they become hard to break. I can empathize with your point of view though. [grin] Anyways, you did ask on the forums, so we're helping as best we can.

Quote:
Original post by Reegan
PS: We seem to talk alot you and i on here, perhaps i should take your IM email address eh? lol. By the way, are you working on or have finished any games recently? i feel like playing something. [smile]


Well, my current project doesn't have any kind of demo right now. I want to at least get the collision done, so people can run around my awesome randomly-generated mazes. [lol] As for something playable, check out EasilyConfused's journal - his Squishy demo is great fun.

You can always poke me in a forum PM if you want, I check in pretty often. [smile]
~Jonathan

Share this post


Link to post
Share on other sites

This topic is 3199 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this