Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Forward Declaration of an enum  (Read 5221 times)

0 Members and 1 Guest are viewing this topic.

TheBrownShape

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Forward Declaration of an enum
« on: August 28, 2015, 06:38:39 pm »
Hello,

I know that the following question is not about SFML, but I also use SFML in this project and I don't want to sign up in another forum - sry. BUT it is only a little question and it would be great if you would help me.

My problem:

I have a class with some member-variables and some member-functions. I also have an enum named "result", it's like "status". It is declared as public. But a private funktion has this enum as return-type, and at this point the compiler "don't know" the enum. Here the code:
class CQueryManager
{
private:
        sf::TcpSocket m_Socket;
        result m_ConvertSocketStatusToResult(sf::Socket::Status Status);
       
public:
        CQueryManager();
       
        enum result {OK, ERROR, DISCONNECTED, NOTREADY};
       
        result connect(sf::IpAddress IP, unsigned short Port, sf::Time Timeout = sf::Time::Zero); ///connects to the ServerQuery
        result sendCommand(std::string Command); ///sends the string to the ServerQuery
        std::string receive(); ///returns received string / returns NULL if there is nothing to receive or received nothing
};
 

So how I tell the compiler that the declaration will come a few lines later ? I tried "enum result;" before class declaration, but it did'nt work. Please help me.

Best regards.
TheBrownShape
« Last Edit: August 28, 2015, 06:40:45 pm by TheBrownShape »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #1 on: August 28, 2015, 06:57:12 pm »
You cannot forward declare enum's.
But you can forward declare 'enum class's (aka strongly typed enums) :)
http://en.cppreference.com/w/cpp/language/enum

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Forward Declaration of an enum
« Reply #2 on: August 28, 2015, 07:20:20 pm »
Move the private stuff at the end, it makes more sense anyway :P
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #3 on: August 28, 2015, 07:30:07 pm »
Laurent is right, of course. I just wanted to advertise strongly typed enums a bit (they really are nicer in many ways) ;)

TheBrownShape

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #4 on: August 29, 2015, 01:02:03 pm »
Sorry Jesper, but I don't understand how to use this in my case. Can you give me an example with code to my specific problem .. ? Don't understand me wrong, I don't want that you write my half program and I do nothing, I just don't understand the thing with enum classes and so on .. :/ How to use this in my case ?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Forward Declaration of an enum
« Reply #5 on: August 29, 2015, 02:07:13 pm »
I agree with moving the private stuff below. That fixes this problem.

However, in your code, to forward declare an enum class would be something like:
class CQueryManager
{
private:
    enum class result;
    result m_ConvertSocketStatusToResult();
public:
    enum class result {OK, ERROR, DISCONNECTED, NOTREADY};
    result connect();
    result sendCommand();
}
I've left out the parts that aren't relavent to the enum class.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheBrownShape

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #6 on: August 29, 2015, 02:41:23 pm »
Okay, and an enum class is nothing else than an enum ? What's the difference ?

//Btw, I don't want to move the private stuff to the end. I learned it in my book like this and I don't want to get used to another :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Forward Declaration of an enum
« Reply #7 on: August 29, 2015, 02:52:35 pm »
an enum class is nothing else than an enum ? What's the difference ?
You must specify an enum class' type whenever you use the value
e.g. result::OK
whereas an enum can ignore it and it spreads into other spaces.
e.g. OK

Btw, I don't want to move the private stuff to the end. I learned it in my book like this and I don't want to get used to another :)
If you're going to get used to using only one way, I would suggest getting used to putting private at the end. Putting public first makes sense as that's the public interface wherease the private section is implementation only so can be at the end.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheBrownShape

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #8 on: August 29, 2015, 04:08:58 pm »
Thank you.

If you're going to get used to using only one way, I would suggest getting used to putting private at the end. Putting public first makes sense as that's the public interface wherease the private section is implementation only so can be at the end.

I use this method (public at the end) since 2 years and for me it makes sense :D I learned it like this. I will not change it now :D.

But thank you for your help!

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Forward Declaration of an enum
« Reply #9 on: August 29, 2015, 07:50:53 pm »
I use this method (public at the end) since 2 years and for me it makes sense :D I learned it like this. I will not change it now :D.

I also used to put public at the end. In the project I'm doing now I decided to move all public at the beginning and i do not regret it :D

I recommend it.


PD: I did not know you could forward declare an enum class :o

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Forward Declaration of an enum
« Reply #10 on: August 29, 2015, 10:33:02 pm »
You must specify an enum class' type whenever you use the value
e.g. result::OK
whereas an enum can ignore it and it spreads into other spaces.
e.g. OK
Yes, and enum class/enum struct values are not implicitly convertible to integers. Which is -- at least in my code -- more often annoying than not, because I regularly use enumerators as indices.

class CQueryManager
Concerning the C prefixes you're using, this is something that made sense in earlier days of programming, but not in 2015. You could read my post here :)

I use this method (public at the end) since 2 years and for me it makes sense :D I learned it like this. I will not change it now :D.
Sounds like an old man with rigid habits who's not willing to learn anything new :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Forward Declaration of an enum
« Reply #11 on: August 30, 2015, 12:05:58 am »
Sounds like an old man with rigid habits who's not willing to learn anything new :P
I was trying to avoid saying this  :-X Hehe
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

TheBrownShape

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Forward Declaration of an enum
« Reply #12 on: August 30, 2015, 11:15:57 am »
No ? I like this type of declaration. There is no reason to learn the other .. give me a reason :D

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Forward Declaration of an enum
« Reply #13 on: August 30, 2015, 12:06:14 pm »
The reason is that the order in which you declare things can be the difference between whether or not your application compiles. You can have multiple access specifiers in a class you know that right? You arent limited to 1 public, 1 private and 1 protected. Double/triple/etc up if you need to but make sure your type forward declarations happen before you use them (Common sense?). I agree with what Nexus and Hapax are saying. They know that they are talking about (A lot more than I do), so maybe it might be a good idea to at least consider what they are saying.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Forward Declaration of an enum
« Reply #14 on: August 30, 2015, 12:30:09 pm »
There is also the fact that if other people are looking at your code, it makes it a lot easier for them to simply see the class name and then immediately after that, there are the things they can do with your class.

 

anything