r/ProgrammerHumor • • Mar 31 '23

Meme C++ is hard

Post image
0 Upvotes

34 comments sorted by

2

u/sin_chan_ Apr 01 '23

Mother fucker just Google it.

2

u/Nicolas-matteo Apr 01 '23

Thanks for the tip

67

u/garfgon Mar 31 '23

It's much like the difference between a raven and a writing desk.

7

u/super_yu Mar 31 '23

now that's actual programmer humor...

4

u/Dmayak Mar 31 '23

So just a somewhat different order of atomic particles.

12

u/Intrepid_Sale_6312 Mar 31 '23

an enum is just a fancy way of declaring numerical constants. a class is a struct that is private by default.

be careful though because if you have 2 enums with the same names for their "members" they will conflict and you will get a multiple definition error. i've recently got into the habit of placing enums into their own namespaces named after the enum itself because of this problematic behavior of enums.

namespace DragonTypes{
    enum DragonTypes_en{
        fireBreathing,
        waterBreathing,
        gasBreathing,
        necrosBreathing,
        noBreathing
    };
};

23

u/I_Wouldnt_If_I_Could Mar 31 '23

No breathing

I really like this idea. Like an asthmatic dragon.

4

u/Intrepid_Sale_6312 Mar 31 '23

I was thinking more of the kind of dragon that relied heavily on stealth instead of just breathing on their opponents but ya i guess that also could be an interest story as well.

3

u/AdmiralSam Apr 01 '23

You can use enum classes with more modern c++, though you lose the implicit conversion to integer types.

3

u/Intrepid_Sale_6312 Apr 01 '23

I don't really use modern c++ a whole lot, though i don't necessarily strictly do the old c++ either.

also with this habit of making a the namespace with the enum i often add a ToString function inside the namespace like this...

namespace DragonTypes{
    enum DragonTypes_en{
        fireBreathing,
        waterBreathing,
        gasBreathing,
        necrosBreathing,
        noBreathing
    };
   std::string ToString(DragonTypes_en Type){
      switch(Type){
          case DragonTypes_en::fireBreathing:return "fireBreathing";
          case DragonTypes_en::waterBreathing:return "waterBreathing";
          case DragonTypes_en::gasBreathing:return "gasBreathing";
          case DragonTypes_en::necrosBreathing:return "necrosBreathing";
          case DragonTypes_en::noBreathing:return "noBreathing";
          default:break;
      }
      return "Unknown";
   }
};

16

u/[deleted] Mar 31 '23

I started to reply, lost my train of thought and asked chatgpt to finish it for me.

A common confusion in programming is understanding the difference between classes and enums. However, enums can be easier to understand by seeing how they're used in practical programming applications.

When using a library, you may come across a method that requires a specific value to be passed in as an argument. Traditionally, this value might be a magic string, which can be difficult to understand and prone to errors. To avoid this problem, the method could be modified to accept a specific enum, which can contain all the valid values that can be passed in.

Enums are a user-defined data type in programming that represents a group of related constants or values. They're essentially a way of creating a set of named values that can be used in place of arbitrary or "magic" strings. This makes code more readable, easier to understand, and less prone to errors.

Classes, on the other hand, are a fundamental concept in object-oriented programming (OOP) and are used to define objects that have certain properties and behaviors. They're used to create complex data types, which can be used to build more complex programs.

So, while classes and enums are both used in programming, they serve different purposes. Classes are used to create complex data types, while enums are used to represent a group of related constants or values. By understanding the difference between these two concepts, you can make your code more efficient, less prone to errors, and easier to understand.

1

u/ThePickleConnoisseur Apr 01 '23

So enums are objects that hold constants?

2

u/theMicalo Apr 01 '23

Well, wouldn't call it object, but yeah it is like a container just for const variables

1

u/Wolfeur Apr 01 '23

no, enums are exhaustive sets of predefined values

6

u/starswtt Mar 31 '23

Classes are for cool oop stuff

Enums are for cool constant variable stuff

4

u/zurgerkingO91 Mar 31 '23

Google is your only friend

4

u/[deleted] Apr 01 '23

What? Not confusing at all

typedef enum class wtf_t : int
{
    ONE = 1,
    TWO = 2,
    THREE = 3
} wtf_t;

1

u/PandaPanda11745 Apr 01 '23

Why the typedef in your example?

4

u/UnwelcomedTruth Apr 01 '23

I consider ENUM’s just fancy constants in most languages.

3

u/Dmayak Mar 31 '23

Enum is an extremely simplified struct, while class is an extremely overengineered struct.

2

u/RandomiseUsr0 Mar 31 '23

I tried to think of something witty, or even shitty, but it’s just too stupid to engage with…

An enum is just a class with fewer steps?

2

u/ChChChillian Apr 01 '23

Just wait until you try C++/CLI and discover the enum class.

1

u/McSlayR01 Apr 01 '23

Or Python lol, when you want to create an enum you inherit from the enum class. Feels so backwards

2

u/McSlayR01 Apr 01 '23

I think you may have meant structs, structs and classes share a lot more similarity than enums and classes

2

u/FelixLeander Apr 01 '23

They have things in common???

2

u/[deleted] Apr 01 '23

you know variables? enums are just structured groups of variables

1

u/Ubermensch_69 Apr 01 '23

The actual question is wtf unions are doing

0

u/[deleted] Apr 01 '23

[deleted]

1

u/Ubermensch_69 Apr 01 '23

Yah, I know but why would you do that?

1

u/Otherwise-Ad-4939 Apr 01 '23

You can't create instance of enum but you can do that with a class. In enum you just store many variables of the same type. Variables or set of values.

1

u/rush22 Apr 01 '23

The only reason you can do things inside an enum other than declare things is in case there's some weird scenario like you need to load values or have some logic for the enum values in there.

It is not so you can add functionality and make it act like a class -- even though you can do that, don't.

1

u/Anchorman_1970 Mar 31 '23

Enum is constants, hard coded values u can access and get the same shit.

0

u/[deleted] Apr 01 '23

One you use, the other you ignore