Calling non static function with function pointer syntax

Hello i have problem about the syntax how to call non static member function using function pointer.

suppose my clone function will call the object factory create object function base on the object "original" given.

// should point to the create function
return original != nullptr ? Object* (ObjectFactory::*)(* original) : nullptr;

below is the given task for my assignment

Cloning – Task 2
An unfortunate design omission from the Object class hierarchy is the ability to clone an object: make (deep) copy of a geometric object. To make up for this deficiency, you are to implement a function which will take an Object pointer returned by one of the factory object creation functions and make a new object that has the same color and geometric properties as the original object. That is, the function has prototype
Object *clone(Object *original);

Here, original is an Object pointer, assumed to be the return value of one of the functions CreatePoint, CreateCircle, CreateSquare, or CreatePolygon from the ObjectFactory class. Note that we do not allow cloning of the Background object, in such cases, the Clone function should result in an exception being thrown. The exact exception thrown is not If original is not a pointer of the expected type, the function should return the null pointer; otherwise, it returns a pointer to an object of the same type that is a clone of the original. Thus the code fragment

Object* Clone(Object* original); ObjectFactory of(400, 400, Object::VectorType(1, 1)); Object *obj = of.CreateCircle(Object::PointType(0, 0), 0.5f, ObjectFactory::GetColor(255, 0, 0)), *obj2 = Clone(obj);

will produce two circle objects, both with same radius centered at the same origin, same depth , and color red. There are some things to note. In the case of the Polygon object, you will need to make a deep copy of the polygon vertices in order to call the CreatePolygon factory member function. Warning! The ObjectFactory class makes use of the Win32 API, which has a function named Polygon. Since we have a class named Polygon in our Object hierarchy, there is a naming conflict. To resolve this, you usually you will have to use the struct keyword in order to tell the compiler that you mean the Polygon class, not the function. However, if you have designed the Polygon clas carefully, there is less need of this. For this part of the assignment, you will submit a single source file named Clone.cpp, Object.hpp, Object.hpp and ObjectFactory.hpp, which implements the above function. The following constraints apply to your implementation of Clone:

• The prototype for the Clone function is already declared in the ObjectFactory.hpp file, and you may only include this file.

• You may modify ObjectFactory.hpp but any modifcations should not cause any warnings or errors when doing compilation with ObjectFactory.cpp

• You may not use any selection statements such as switch, if or if-else. However, you are allowed to use one ternary statement.

• You may modify Object.cpp and Object.hpp. However, your modifications should not break other ObjectTest codes that we have previously used.


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
ObjectFactory& ObjectFactory::operator=(const ObjectFactory&)
{
	return *this;
}


Object* Clone(Object *original)
{
	return original != nullptr ? Object* (ObjectFactory::*)(* original)  :  nullptr;
}  

  class ObjectFactory {
public:
    typedef Object::VectorType VectorType;
    typedef Object::PointType PointType;
    typedef unsigned char Byte;

    ObjectFactory(int width, int height, const VectorType& extents);
    ~ObjectFactory(void);
    void WriteToBitmap(const char *name);

    Object* CreatePoint(const PointType& P, unsigned color, float d=1);
    Object* CreateCircle(const PointType& C, float r,
                         unsigned color, float d=1);
    Object* CreateSquare(const PointType& C, const VectorType& v,
                         unsigned color, float d=1);
    Object* CreatePolygon(const PointType* points, int npoints,
                          unsigned color, float d=1);
    Object* CreateBackground(const char *data, int w, int h, float d=0);

    static unsigned GetColor(Byte r, Byte g, Byte b);
    static char* ReadFromBitmap(int *w, int *h, const char *name);

    class RenderingInfo {
    public:
        RenderingInfo(ObjectFactory& obj_factory, unsigned color);
        ObjectFactory& GetObjectFactory () const;
        unsigned GetColor () const ;
    private:
        ObjectFactory& object_factory;
        unsigned color;
    };
private:
    struct RenderedPoint : Point, RenderingInfo {
        RenderedPoint(ObjectFactory& of, const PointType& P,
                      unsigned color, float d=1);
        void Render();
    };
    struct RenderedCircle : Circle, RenderingInfo {
        RenderedCircle(ObjectFactory& of, const PointType& C,
                       float r, unsigned color, float d=1);
        void Render();
    };
    struct RenderedSquare : Square, RenderingInfo {
        RenderedSquare(ObjectFactory& of, const PointType& C,
                       const VectorType& v, unsigned color, float d=1);
        void Render();
    };
    struct RenderedPolygon : Polygon, RenderingInfo {
        RenderedPolygon(ObjectFactory& of, const PointType* points,
                        int npoints, unsigned color, float d=1);
        void Render();
    };
    struct RenderedBackground : Background, RenderingInfo {
        RenderedBackground(ObjectFactory& of,
                           const char *pixel_data, int w, int h, float d=0);
        void Render();
    };
    POINT ToDevice(const VectorType& v) const;
    HBITMAP bitmap;
    HDC context;
    int width, height;
    void *bitmap_data;
    float x_factor, y_factor;
    ObjectFactory(const ObjectFactory&);
    ObjectFactory& operator=(const ObjectFactory&);
};
// implemented in Clone.cpp
Object* Clone(Object *original);


the folder
https://drive.google.com/file/d/1vb9biCfxCavsZCfs8HrCmJNoY0haaEP1/view?usp=sharing
Last edited on
> how to call non static member function using function pointer.
to call a member function you need an object.
then you do (object.*function_pointer)(params)


1
2
3
4
Object* Clone(Object *original)
{
	return original != nullptr ? Object* (ObjectFactory::*)(* original)  :  nullptr;
}

¿what do you think you are doing there?
¿what are you returning?

> below is the given task for my assignment
¿what did you understand of that wall text? ¿what's your approach?


> the folder
> https://drive.google.com/file/d/1vb9biCfxCavsZCfs8HrCmJNoY0haaEP1/view?usp=sharing
I don't care about your binaries, upload your sources
Last edited on
Topic archived. No new replies allowed.