Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Using a class encapsulated enum without fully qualifying?  (Read 2068 times)

0 Members and 1 Guest are viewing this topic.
Using a class encapsulated enum without fully qu...
« on: March 20, 2008, 05:21:26 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
Say I have a class such as:

Code: [Select]
typedef class _myClass
{
public:
enum Directions { UP = 0, DOWN, LEFT, RIGHT };
}MyClass;

When trying to get at one of the directions, I'd have to get at it by:

MyClass::UP

Is there a way I can scope things in, so that I can have access to it by just using UP?

Thoughts?
Logged
  • My Myspace?
Re: Using a class encapsulated enum without full...
« Reply #1 on: March 20, 2008, 09:05:52 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 2245
#define UP MyClass::UP

?
Logged
Re: Using a class encapsulated enum without full...
« Reply #2 on: March 20, 2008, 05:09:15 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
All you have to do is type in UP so long as the variable your assigning it to is an int.

int x = UP;

should work just fine, it does when I do it.
Logged



i love big weenies and i cannot lie
Re: Using a class encapsulated enum without full...
« Reply #3 on: March 20, 2008, 05:18:09 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
#define UP MyClass::UP

?

This would work, but not quite the solution I'm going for. I'd rather have something done by the compiler, rather than the preprocessor. Thanks though!

All you have to do is type in UP so long as the variable your assigning it to is an int.

int x = UP;

should work just fine, it does when I do it.

Maybe it's my compiler, but it sure doesn't work for me.
Screenshot:
Logged
  • My Myspace?
Re: Using a class encapsulated enum without full...
« Reply #4 on: March 20, 2008, 07:41:22 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 266
It's because you're trying to use the enum that you declared in the class, outside of the class. I personally, would define my enum globally in this case.
Logged
Intel P4 3.2 GHZ
2.5GB SDRAM DDR400
350GB SATA
ATI RADEON HD 2600PRO 512MB
Creative Sound Blaster Audigy2 Z
Windows XP SP2

Current Projects: None.
- Trask
Re: Using a class encapsulated enum without full...
« Reply #5 on: March 20, 2008, 07:53:59 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Quote
Quote from: MG-Zero on Today at 01:09:15 PM
All you have to do is type in UP so long as the variable your assigning it to is an int.

int x = UP;

should work just fine, it does when I do it.

Maybe it's my compiler, but it sure doesn't work for me.
Screenshot:

sorry I made an error >.<

I forgot to mention you'll have to make an instance of the enumeration, and since it's in the class you'll have to do the same for that to and then use the UP value for that enumeration.  Do what Trask said and make the enum global, that way you won't need a class instance.  So you can just do...
Code: [Select]
Direction Direction;

int x = Direction.UP;
Logged



i love big weenies and i cannot lie
Re: Using a class encapsulated enum without full...
« Reply #6 on: March 20, 2008, 08:04:31 pm »
  • The Broken King
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1259
@nitz: I think to do what you're trying to do in the screenshot post, you'd have to access the enum through teh scope resolution operator (for example, _myClass::UP), and even then, the enum might have to be static, I'm not too sure about that though [I've been using C# for a while and tiny details like that I have forgotten]
Logged
  • Broken Kings [Temp Site]
Re: Using a class encapsulated enum without full...
« Reply #7 on: March 21, 2008, 12:55:21 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
@nitz: I think to do what you're trying to do in the screenshot post, you'd have to access the enum through teh scope resolution operator (for example, _myClass::UP), and even then, the enum might have to be static, I'm not too sure about that though [I've been using C# for a while and tiny details like that I have forgotten]

That was the point of my original post: I want to get at it without using the SRO. It doesn't have to be static, though.

It's because you're trying to use the enum that you declared in the class, outside of the class. I personally, would define my enum globally in this case.

That was the point. I want the enum to be in the class. The trick is, I want to get access to it as if it were in a namespace.

Quote
Quote from: MG-Zero on Today at 01:09:15 PM
All you have to do is type in UP so long as the variable your assigning it to is an int.

int x = UP;

should work just fine, it does when I do it.

Maybe it's my compiler, but it sure doesn't work for me.
Screenshot:

sorry I made an error >.<

I forgot to mention you'll have to make an instance of the enumeration, and since it's in the class you'll have to do the same for that to and then use the UP value for that enumeration.  Do what Trask said and make the enum global, that way you won't need a class instance.  So you can just do...
Code: [Select]
Direction Direction;

int x = Direction.UP;

That doesn't work. That may work in C#, but the '.' operator is the direct membership operator; and 'UP' is not a member of 'Directions'. As well, with the enumeration in global scope, you don't need to declare an instance of the enumeration, there already is one - in global scope.
Logged
  • My Myspace?
Re: Using a class encapsulated enum without full...
« Reply #8 on: March 23, 2008, 04:16:07 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
yea, i was a bit off.  I just checked my textbook, although you still have to create an instance of the enum, you dont need the . operator.  And i'm not talking about C#, i'm talking about C++.

edit: from my book:

Code: [Select]
int main()
{
enum days = {Sunday, monday, tuesday, wednesday, thursday, friday, saturday };

days today;

today = monday;

//rest of code for outputting the day here, which is useless for this example..
}

anyway, yea, there it is used properly.
« Last Edit: March 23, 2008, 04:23:26 am by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: Using a class encapsulated enum without full...
« Reply #9 on: March 23, 2008, 11:09:57 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 256
yea, i was a bit off.  I just checked my textbook, although you still have to create an instance of the enum, you dont need the . operator.  And i'm not talking about C#, i'm talking about C++.

edit: from my book:

Code: [Select]
int main()
{
enum days = {Sunday, monday, tuesday, wednesday, thursday, friday, saturday };

days today;

today = monday;

//rest of code for outputting the day here, which is useless for this example..
}

anyway, yea, there it is used properly.

Yeah, but that's still declaring the enum inside of that function. I'm trying to get at it while leaving it in the class declaration.
Logged
  • My Myspace?
Re: Using a class encapsulated enum without full...
« Reply #10 on: March 24, 2008, 12:05:43 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
hmm...how about something like..

Code: [Select]
class classname
{
enum enumname
{
black,
red,
white
};

};

classname object;

object.enumname enuminstnace;


?  Just a guess, i've never worked with an enum this way before.
Logged



i love big weenies and i cannot lie
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.017 seconds with 56 queries.

anything