-1

This is my main file:

#include "MyHeader.h"

int main(void)
{
    MyClass Test1;
    Test1.set_a_thing();

    return 0;
}

This is the Header file:

#ifndef MY_CLASS_H_
#define MY_CLASS_H_

class MyClass
{
public:
    MyClass();
    ~MyClass();

    enum A_few_Things { yes, no, sometimes, dont_know };
    enum MyClass::A_few_Things set_a_thing();

private:
    A_few_Things a_few_things = A_few_Things::dont_know;
};

#endif // !MY_CLASS_H_

And this is the related cpp file:

#include "MyHeader.h"

MyClass::MyClass()
{
}

MyClass::~MyClass()
{
}

MyClass::A_few_Things MyClass::set_a_thing()
{
    // for example, return Yes
    return A_few_Things::yes;
}

The line enum A_few_Things { yes, no, sometimes, dont_know }; seems to have to be public. If it is in the private: field, there is an error message:

E0471 class " * " does not have an identifier with the name " * "

How can you ensure that the enum itself cannot be called from Main.cpp, but that only the function set_a_thing() – which is called from Main.cpp – sees and handles it?

Is there a special keyword?

For example, I want the function set_a_thing to return yes.

I'm fairly new to C++. My only concern is that I want to learn it right from the start. When you say there is no need to do it differently, so then...

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Daniel
  • 374
  • 1
  • 12
  • Just make the function private as well? – Konrad Rudolph Jul 14 '21 at 16:16
  • 1
    Not sure what the point of a public function with a return type of `Wears_Glasses` is if you want to keep the existence of the `Wears_Glasses` enum hidden from calling code. – Nathan Pierson Jul 14 '21 at 16:16
  • @KonradRudolph The function needs to be seen by Main.Cpp. – Daniel Jul 14 '21 at 16:18
  • 2
    Could you edit the question to show an example of how the calling code is supposed to work? "The function needs to be seen by Main.Cpp" is a bit vague to me. – Nathan Pierson Jul 14 '21 at 16:19
  • Function should be public. Please add the function to the above code. From *within the function,* you can access the private field. Are you sure your error does not refer to access to the private field elsewhere ? – Goodies Jul 14 '21 at 16:21
  • 4
    What do you expect people to be able to *do* with the return value of `set_whether_person_wears_glasses` if they can't understand what its return type is? In the most abstract sense, they can't even assign it to a variable, because an abstract type doesn't necessary have a copy constructor. – Silvio Mayolo Jul 14 '21 at 16:21
  • @Daniel Well then the answer is no, you can’t: if the function uses your enum type in its interface, then the enum type’s visibility must be at least as wide as that of the function using it. – Konrad Rudolph Jul 14 '21 at 16:23
  • 2
    Fiddly edge case: You apparently can use `auto` to get around the visibility of the class. See [here](https://godbolt.org/z/ccjha3qE9) for example. This seems like a bad idea. If the function is supposed to be public, the return type of the function should be public. – Nathan Pierson Jul 14 '21 at 16:27
  • *My only concern is that I want to learn C++ right from the start.* Then I highly recommend learning C++ from a [good C++ book](https://stackoverflow.com/a/388282/4641116), especially because C++ is a very large, complex, byzantine, complicated language with lots of odd constructs for backwards support of legacy code. In my assessment, for mainstream languages, it is by far the language with the most difficult learning curve. – Eljay Jul 14 '21 at 16:33
  • Hi all, I've edited my Question. It should be more clear now. – Daniel Jul 14 '21 at 16:35
  • 1
    `enum MyClass::A_few_Things set_a_thing();` doesn't need to return anything, because the caller doesn't use the return value. Change it to `void set_a_thing();`. – Eljay Jul 14 '21 at 16:35

2 Answers2

0

Thanks to all of you and especially to @Eljay You put me on the right path. The enum MyClass::A_few_Things set_a_thing(); doesn't need to return anything. Inside the function, a_few_things is set.

#ifndef MY_CLASS_H_
#define MY_CLASS_H_

class MyClass
{
public:
    MyClass();
    ~MyClass();

    void set_a_thing();

private:
    enum A_few_Things { yes, no, sometimes, dont_know };
    A_few_Things a_few_things = A_few_Things::dont_know;
};

#endif // !MY_CLASS_H_

in the cpp file:

void MyClass::set_a_thing()
{
    a_few_things = MyClass::A_few_Things::dont_know;
}

And, in the Main.cpp, you can call the function as usual: MyClass Test1; Test1.set_a_thing();

Daniel
  • 374
  • 1
  • 12
0
    #include <iostream>

class MyClass
{
public:
    enum ET_A_few_Things { yes = -1 , no , sometimes = 100, dont_know };   //::  ( 1 )
    ET_A_few_Things set_a_thing();                                         //::  ( 2 )
    ET_A_few_Things set_a_thing( ET_A_few_Things );                        //::  ( 3 )

private:
    ET_A_few_Things a_few_things = dont_know;
};

MyClass::ET_A_few_Things MyClass::set_a_thing()
{
    // for example, return Yes
    return ( a_few_things = no );      //::  ( 4 )
}

MyClass::ET_A_few_Things MyClass::set_a_thing( MyClass::ET_A_few_Things NewValue_A )
{
    // for example, return Yes
    return ( a_few_things = NewValue_A );
}



enum class ET_otherThings{ yes , no , sometimes = 73 };       //:: ET_otherThings{ 0 , 1 , 73 };       //::  ( 5 )


void showNumber( ET_otherThings value_A )                                                             
{
    std::cout << static_cast< int >( value_A ) << std::endl;                                             
}

int main()
{  
    MyClass Test1;
    std::cout << Test1.set_a_thing() << std::endl;                            //:: result = 0
    std::cout << Test1.set_a_thing( MyClass ::sometimes ) << std::endl;       //:: result = 100
    // Test1.set_a_thing( ET_otherThings:: sometimes )                         //:: Error: wrong argument
    // showNumber( MyClass:: sometimes );                                      //:: Error: wrong argument
    showNumber( ET_otherThings::sometimes );                                   //:: result = 73

    return 0;
}

*( 1 ) :: Keep in mind: Enum is numeric type. Each value is a number. From default numeration start is 0. In this example start as -1.

*( 2 ) :: Here use only type name "ET_A_few_Things". "enum" use only for creating. Inside a "MyClass" don't use "MyClass ::" becouse without this code look cleaner.

*( 3 ) :: Overloaded function that allow pick a value.

*( 4 ) :: First set value, next return new value. Its class function so you dont have to use ET_A_few_Things::no. Invisible pointer make work done corrently: " this -> a_few_things = this -> no; "

*( 5 ) :: enum class - other method for creating enumType. Inside class its fine to use "enum" like in ( 1 ) in other case better option is pick "enum class".

If you want access for using this enum type only from class put this type in private or protected access specifier