Hi, in my code below if I move scope protected below public just as I would like to see it this code won't compile. I always thought that order of members in class is without any importance but here I see something different or I'm doing something wrong? Thank you.

class Object
    {
        protected:
        enum ID {OBJECT, CAVE, MONSTER, WUMPUS, HERO, GUN, BAT};
        Coordinates* _my_coordinates;
        void id(ID id)
        {
            this->_my_id = id;
        }
    public:
        explicit Object(s_int coordinate_x = 0, s_int coordinate_y = 0);
        virtual void move(Direction, u_int distance = 1) = 0;
        Object::ID id() const
        {
            return _my_id;
        }
        const Coordinates* location() const 
        {
            return _my_coordinates;
        }
        virtual ~Object(void);

    private:
        ID _my_id;
    };

Recommended Answers

All 3 Replies

You have two same functions in class

class Object
{
protected:
enum ID {OBJECT, CAVE, MONSTER, WUMPUS, HERO, GUN, BAT}; //maybe it better place outside the class?
Coordinates* _my_coordinates;
void id(ID id) //first declaration
{
this->_my_id = id;
}
public:
explicit Object(s_int coordinate_x = 0, s_int coordinate_y = 0);
virtual void move(Direction, u_int distance = 1) = 0;
ID getId() const //redeclaration(renamed)
{
   return _my_id;
}
const Coordinates* location() const 
{
   return _my_coordinates;
}
virtual ~Object(void);

private:
    ID _my_id;
};

this is a reformed code

Use get- and set- methods, like setId(), and getId()

void setId(ID new_id)
{
    this->_my_id = new_id;
}

ID getId() const
{
    return this->_my_id;
}

P.S. use [ code = cpp ] for c++ code.

I always thought that order of members in class is without any importance but here I see something different or I'm doing something wrong?

In some patterns the order that you define variables is important. For example, when you initialize member variables in an initialization list:

class MyClass
{
int A,B,C;
public:
MyClass(int a, int b, int c) : A(a), B(b), C(c) {}
};

Some compilers will give you warnings if you switch the order, i.e. MyClass(int a, int b, int c) : A(a), B(b), C(c) {} I think it is because sometimes constructing B depends on A already having been constructed. As for where public, private, protected blocks go, it doesn't matter- in fact you can have multiple of the same block:

class MyClass
{
private:
int A,B,C;
public:
MyClass(int a, int b, int c) : A(a), B(b), C(c) {}
private:
int D;
};

Dave

You have two same functions in class

class Object
{
protected:
enum ID {OBJECT, CAVE, MONSTER, WUMPUS, HERO, GUN, BAT}; //maybe it better place outside the class?
Coordinates* _my_coordinates;
void id(ID id) //first declaration
{
this->_my_id = id;
}
public:
explicit Object(s_int coordinate_x = 0, s_int coordinate_y = 0);
virtual void move(Direction, u_int distance = 1) = 0;
ID getId() const //redeclaration(renamed)
{
   return _my_id;
}
const Coordinates* location() const 
{
   return _my_coordinates;
}
virtual ~Object(void);

private:
    ID _my_id;
};

this is a reformed code

Use get- and set- methods, like setId(), and getId()

unfortunately, that is completely preference, many people use function overloading for their respective getters and setters

void RandomObject::Value(int newValue) { _value = newValue; } //setter
int RandomObject::Value() { return _value; } //getter

this is entirely correct. Note the different function signatures? There's no problem with how that is set up. BUT, if you did feel like making the switch to the explicit get-set functions, don't forget to change the setter too ;)

~J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.