is it possible to nest to class existing enum - c++

I have enum in some header file. Is it possible to nest to the class existing enum?
Explanation:
some headerfile.h:
enum someEnum
{
someValue
/*other values*/
};
other header:
#include "headerfile.h"
class someClass
{
public:
//using enum someEnum; //don't work as I want
};
I want that someValue will be accesible as
someClass::someValue
So my question is it possible?

You can nest that enum definition:
class someClass {
public:
enum someEnum {
someValue
};
};
Then you can access this enumerations just like the way you wanted:
someClass::someEnum X = someClass::someValue;
If, however, what you wanted was to create a member variable typed someEnum, you can do it either by just supplying someEnum as a type, or nesting the enumeration and putting the variable name before the semicolon.

well one way would be to do this:
class someClass
{
public:
#include "headerfile.h"
//using enum someEnum; //don't work as I want
};
not pretty but works.

You could try to include the library inside the class definition.

Related

Forward Declaration of enum in a class ?

I have two Abstract Class wich own each other a pointer of the other one. I need to use in one of them an enum of the other Class, like show the exemple.
AFoo is holding ABar, and ABar need a pointer to AFoo to update some data and also a member function that gonna use the AFoo enum.
I remember having this problem once but not with enum and i ended up doing inline declaration.
I could do a nested class, but is there another way to avoid that?
AFoo.hpp :
#include ...
class AFoo;
enum AFoo::poo; --> not possible
#include "ABar.hpp"
class AFoo {
public:
...
virtual void func() = O;
enum poo {
H,
I,
...
}
protected:
ABar *bar_;
};
ABar.hpp
#include ...
class Abar;
#include "AFoo.hpp"
class ABar {
public:
...
virtual AFoo::poo doing_some_stuff() = 0; --> Here is my problem (if i replace the return type with basic type i have no compilation problem)
protected:
AFoo *foo_;
};
In AFoo.hpp, don't include ABar.hpp, only forward declare the class:
class ABar;
Also, in ABar.hpp, include AFoo.hpp and don't forward ABar or AFoo.
In AFoo.hpp, remove
enum AFoo::poo; --> not possible
and replace
#include "ABar.hpp"
with
class ABar;
Also, your enum declaration is missing a semicolon.

How to make a function that receives an enum

class Shapemaker
{
public:
static Shape * shapeCreate(CDrawView::shape sh);
};
My enum on my CDrawView class is
enum shape{line, rect, elli};
shape current_shape;
when i call Shapemaker::shapeCreate(current_shape) on I get error c2653CDrawView : is not a class or namespace name on shapemaker.h
This is probably the most plain thing to do:
class Shapemaker{
public:
enum Color { //your colors here }
};
class Otherclass{
void fun(Shapemaker::Color);
};
Now if your compiler does not recognize Shapemaker as a class name, that makes me think you didn't include its header file before declaring Otherclass.
If it's an enum member of the other class, then you can reference it as nameoftheClass::Color, but it'd have to be publicly-visible:
void function(nameoftheClass:Color input);
Say you have as follows:
class C {
public:
enum E {
HERP,
DERP
};
};
A function taking that enum would look like:
void foo(C::E e) {
// do stuff with e
}
It's all a matter of namespace in the end. Have a look at the answers to this question too, definitively you must not use the keyword enum in the function's parameter list, use directly the enum name with the appropriate namespace.
namespaces for enum types - best practices
i tried to pass the color variable enum, but it gives me a compile error saying that the "nameoftheclass" is not a class or namespace
You need to have a declaration placed before you use it, meaning you need proper header files:
MyClass.h
class MyClass {
public:
enum Color {
Red,
Green,
Blue
};
};
MyOtherClass.h
#include "MyClass.h" // This is required.
// Now you can use MyClass::Color freely.

Forward declare a inner class type, is it possible?

Ok here is the situation.
//foo.h
struct A1{
struct A2{};
};
//bar.h
#include "MyString.h"
class A2; //note, not including foo.h
TEMPLATE_INSTIANTIATE_MAP_OF_TYPE(String,A2*); //assume compiler doesn't do this
Is it possible to make the above situation work? I try to create a MyMap<String,A1::A2*> m; but the compilers throws undefined reference errors. Is it possible to make the above work without having bar.h import foo.h?
Sadly, it isn't. Nested classes can only be declared inside a class definition.
Here is a way to declare nested classes outside a class definition.
class Logic is the outer class. LogicImp is the forward declared struct.
class Logic
{
public:
Logic();
~Logic();
private:
struct LogicImp;
std::unique_ptr<LogicImp> limp_;
};
struct Logic::LogicImp
{
int nLogical_;
};
Logic::Logic():limp_(new LogicImp())
{
}
Logic::~Logic()
{
}

typedef'ing an enum does not make the enum-values visible

I have a class in which I have an enumeration, defined like this:
class X
{
public:
enum Direction {DIR_LEFT, DIR_RIGHT};
};
Now I want this enumeration to be reused in another class, like this:
class Y
{
public:
typedef X::Direction Direction;
};
As expected, using Y::Direction works correctly, e.g.:
void myFunction (Y::Direction dir)
{
}
But the values within the enumeration does not seem to be 'copied' together with the typedef. If I write the following, I get compilation errors:
myFunction (Y::DIR_LEFT);
Instead, I have to refer to the original place of the enumeration again, like this:
myFunction (X::DIR_LEFT);
Which defeats my purpose of typdefing the enumeration.
The only solution I see is to move the enumeration out of class X, and putting it in another class (e.g. MyEnums), so it can be reused by X and Y (although they should still use MyEnums::DIR_LEFT and MyEnums::DIR_RIGHT), but at least the code does not depend on class X anymore.
Why are the enumeration values itself no exposed via the typedef?
Are there any other patterns to manage enumerations in different classes?
Unfortunately C++ doesn't introduce a new scope with an enum although C++0x is improving things.
Practically this means that you can't typedef an enum and get the enumerated values as well.
What you can do is use a nested struct with the name you want for the enum and typedef THAT.
class X
{
public:
struct Direction { enum EnumType {LEFT, RIGHT}; };
};
class Y
{
public:
typedef X::Direction Direction;
};
Now you can do:
myFunction (Y::Direction::LEFT);
The purpose of the nested struct is to create a "fake" scope to holld both the enum name and its values.
Here is my understanding of how enums work in C++. (Or at least my observed behaviour of enums in Microsoft Visual C++.)
The enum keyword does not create a scope the same way that classes do.
The full name then for your enum 'Direction', is X::Direction. The values within that enum are still part of the class scope, so they are X::DIR_LEFT and X::DIR_RIGHT.
When you typedef the enum in another class, this does not change the scope of the values of the enum.
I suggest you put the enum inside a namespace in a header file if you want to share it in multiple locations.
If you want the enum values to be members of both classes, the
solution is to define a separate class with the enum, and
inherit from it, e.g.:
class MyEnums
{
protected:
~MyEnums() {} // Prevent delete through pointer to this class
public:
enum Direction
{
DIR_LEFT,
DIR_RIGHT
};
};
class X : public MyEnums
{
// ...
};
class Y : public MyEnums
{
// ...
};
Users will see X::Direction, X::DIR_LEFT and Y::Direction,
Y::DIR_LEFT. Of course, they'll still be able to pass
a Y::DIR_LEFT to a function expecting an X::Direction; to
prevent that, make MyEnums a template, with the derived class as
the template argument.
Anything shared by more than one class should be factored outside of the classes and perhaps into a parent class.
direction.hpp:
#ifndef DIRECTION_HPP
enum Direction {DIR_LEFT, DIR_RIGHT};
#endif
x.hpp:
#ifndef X_HPP
#include "direction.hpp"
class X
{
Direction dir;
};
#endif // X_HPP
y.hpp
#ifndef Y_HPP
#include "direction.hpp"
class Y
{
Direction dir;
};
#endif // Y_HPP
If the original declaration:
class X
{
public:
enum Direction {DIR_LEFT, DIR_RIGHT};
};
is embedded in a large legacy code-base, then we might want a solution that does not change any existing uses of X::Direction. In that case, the rather ugly:
class Y
{
public:
typedef enum X::Direction Direction;
static const enum X::Direction DIR_LEFT = X:DIR_LEFT;
static const enum X::Direction DIR_RIGHT = X:DIR_RIGHT;
}
works...
Definitely not recommended for new code, however!

C++ Why am I unable to use an enum declared globally outside of the class it was declared in?

Right now, my project has two classes and a main. Since the two classes inherit from each other, they are both using forward declarations. In the first object, right underneath the #include statement, I initialize two enums, before the class definition. I can use both enums just fine inside that class. However, if I try to use those enums in the other class, which inherits from the first one, I get an error saying the enum has not been declared. If I try to redefine the enum in the second class, I get a redefinition error.
I have even tried using a trick I just read about, and putting each enum in its own namespace; that didn't change anything.
Here's an example:
#ifndef CLASSONE_H
#define CLASSONE_H
namespace Player
{
enum Enum
{
One,
Two,
};
}
#endif
Then inside the second class, I attempt to use the enum declared earlier:
void AddPlayer(Player::Enum playerNumber);
and instead get an error saying 'Player' has not been declared.
I'm not sure what issue you are having without seeing your code, but this compiles:
enum OutsideEnum
{
OE_1,
OE_2,
};
namespace ns
{
enum NSEnum
{
NE_1,
NE_2,
};
}
class Base
{
public:
enum BaseEnum
{
BE_1,
BE_2,
};
void BaseFunc();
};
class Derived
{
public:
enum DerivedEnum
{
DE_1,
DE_2,
};
void DerivedFunc();
};
void Base::BaseFunc()
{
BaseEnum be = BE_1;
Derived::DerivedEnum de = Derived::DE_1;
OutsideEnum oe = OE_1;
ns::NEEnum ne = ns::NE_1;
}
void Derived::DerivedFunc()
{
Base::BaseEnum be = Base::BE_1;
DerivedEnum de = DE_1;
OutsideEnum oe = OE_1;
ns::NEEnum ne = ns::NE_1;
}
int main()
{
Base::BaseEnum be = Base::BE_1;
Derived::DerivedEnum de = Derived::DE_1;
OutsideEnum oe = OE_1;
ns::NEEnum ne = ns::NE_1;
}
Two things to watch for with enums defined inside a class definition:
Make sure it's declared public if you want it publicly available.
When referencing it from anywhere other than the class it's defined in, use the class name to qualify the name of the enum and the values.
EDIT:
Ok, the problem has nothing to do with enums, but rather order of inclusion, when you have a base class and a derived class, only the derived class needs to know about the base class:
Base class header:
#ifndef BASE_H
#define BASE_H
enum BaseEnum
{
};
class Base
{
};
#endif
Derived class header:
#ifndef DERIVED_H
#define DERIVED_H
#include "Base.h"
class Derived
{
void Func(BaseEnum be);
};
#endif