returning value using a pointer and struct

I'm trying to have my function return the current position of the person but I'm having trouble figuring out the proper way to return the value because it's a pointer value that calls a struct.


1
2
3
4
5
6
7
8
9
10
  #include <stddef.h>

struct Position
{
    size_t x;
    size_t y;
    
    Position(size_t x, size_t y) :x{x}, y{y} {}
    Position(Position& other) :x{other.x}, y{other.y} {}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "position.h"

// The base class of Guards and Innocents and is abstract
class Person
{
public:
    /** Person object. the base class for guards and Innocents
     * @parameter pos. the initial position
     * @parameter type. the type of person
     */
    Person(const Position& pos, char type);
    
    /** Gets the current position of the Person
     * @return position. */
    Position getPosition() const;

    /** Gets the type of the person [I, R, H, Y, W]
     * @return the type of the Person
     */
    char getType() const; 

    /** Equality operator
     * @parameter other the other person object
     * @return true if this object has the same position as the other and false otherwise */
    bool operator==(const Person& other) const;

    /** Inequality operator
     * @parameter other the other person object
     * @return false if this object has the same position as the other and true otherwise */
    bool operator!=(const Person& other) const;

    /** Moves the Person from the current position to pos. This is a pure virtual function
     * @parameter move determines the direction to move. The default value is 'A' for auto  */
   virtual void move(char move = 'A') = 0; 

    /** Virtual Destructor */
    ~Person(){}
    
protected:
    Position* pos;
    char type;

private:
    
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "person.h"

Person::Person(const Position &pos, char type)
    : type() {}

Position Person::getPosition() const
{
    return pos(new size_t(x, y)); //i want this to return the position
}
char Person::getType() const
{
    return type;
}

bool Person::operator==(const Person &other) const
{
    return (*this == other);
}
bool Person::operator!=(const Person &other) const
{
    return (*this != other);
}

void Person::move(char move = 'A')
{

} // should be pure virtual

given that copying an integer is cheap, can you just return a <pair> to the user?
this all seems to be working right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <iostream>
using namespace std;

struct Position
{
	size_t x;
	size_t y;

	Position():x(0),y(0) {}
	Position(size_t x, size_t y) :x{ x }, y{ y } {}
	Position(const Position& other) :x{ other.x }, y{ other.y } {}

	Position GetPos() const {
		return Position(*this);
	}

};

class person {
public:
	Position p;
	person() {}
	person(int x,int y) : p(x,y) {}

	Position GetPos(person& per) const  {
		return Position(per.p);
	}
	Position GetPos() const {
		return Position(this->p);
	}
	
};

int main() {
	person p(234, 543);
	auto pos = p.GetPos();
	cout << pos.x << " " << pos.y;

	person abc(43, 76767);

	auto y = person().GetPos(abc);
	cout << "\n" << y.x << " " << y.y;

	auto h = p.p.GetPos();
	cout << "\n" << h.x << " " << h.y;
}
Topic archived. No new replies allowed.