Can i use = between class instances?

I need to change a class instance to another instance of the same class.
I'm also using SDL;
Here's my code (Or most of it):

Item.h:
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
  #ifndef ITEM_H_INCLUDED
#define ITEM_H_INCLUDED
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "Items.h"

class ITEM;

class Item
{
    private:
    float X,Y;
    int W,H;
    int OX,OY;
    const char* imgAddr;
    const char* Name;
    int TYPE;
    int damage,attackSpeed,minDamage;
    SDL_Surface* surface;

public:
    Item();
    ~Item();
    Item(int x,int y,int w,int h,const char* name,const char* img_addr);
    Item(int x,int y,int w,int h,const char* name,const char* img_addr,int flags);

    void draw(SDL_Surface* window);
    void move_to(int,int);
    void flag(int flag);
    bool contains(int ,int);

    void setWeapon(int Damage,int AttackSpeed);

    int getX();
    int getY();
    int getH();
    int getW();
    int getOX();
    int getOY();
    int getItemType();
    const char* getImgAddr();
    const char* getName();
    void drawName(SDL_Surface* window,int x,int y);

    ITEM getInstance();

    int getDamage();
    int getAttackSpeed();
};

#endif // ITEM_H_INCLUDED 


Item.cpp:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <iostream>
#include <windows.h>
#include <sstream>
#include "Item.h"

#define IT_BASE 0
#define IT_HELMET 1
#define IT_CHESTPLATE 2
#define IT_PANTS 3
#define IT_BOOTS 4
#define IT_ACCESSORRY 5
#define IT_WEAPON 6

Item::Item()
{

}

Item::~Item()
{
    W=0;
    H=0;
    Name = "";
}

Item::Item(int x,int y,int w,int h,const char* name,const char* img_addr,int flags)
{
    X=x;
    Y=y;
    W=w;
    H=h;
    imgAddr=img_addr;
    Name=name;
    TYPE = flags;
    OX=0;
    OY=0;
    SDL_Surface* loadedImg = IMG_Load(imgAddr);
    surface = SDL_DisplayFormatAlpha(loadedImg);
    SDL_FreeSurface(loadedImg);
}

ITEM Item::getInstance()
{
    ITEM item;
    switch(TYPE)
    {
    case IT_BASE:
        item = ITEM(Name,imgAddr,TYPE);
    break;

    case IT_WEAPON:
        item = ITEM_WEAPON(Name,imgAddr,damage,attackSpeed);
    break;
    }

    return item;
}

void Item::drawName(SDL_Surface* window,int x,int y)
{
    if(strlen(Name) > 0)
    {
        SDL_Rect dst;
        dst.x=x;
        dst.y=y;
        dst.h=16;
        dst.w=32;

        SDL_Color textColor = {255,255,255};

        std::stringstream text_to_draw;
        text_to_draw<<Name;

        switch(TYPE)
        {
        case IT_WEAPON:
            text_to_draw<<" "<<minDamage<<"-"<<damage;
        break;
        }

        std::string Text = text_to_draw.str();
        const char* drawFinale = Text.c_str();

        TTF_Font* font = TTF_OpenFont("font.ttf",18);
        SDL_Surface* text = TTF_RenderText_Solid(font,drawFinale,textColor);

        SDL_BlitSurface(text,NULL,window,&dst);
    }
}

void Item::setWeapon(int Damage,int AttackSpeed)
{
    damage = Damage;
    attackSpeed = AttackSpeed;
    TYPE = IT_WEAPON;
    minDamage = (int)damage/2;
}

bool Item::contains(int x,int y)
{
    bool retVal = false;

    if(x>=X)
    {
        if(x<X+W)
        {
            if(y>=Y)
            {
                if(y<Y+H)
                {
                    retVal = true;
                }
            }
        }
    }

    return retVal;
}

void Item::draw(SDL_Surface* window)
{
    SDL_Rect src;
    src.x=0;
    src.y=0;
    src.w=W;
    src.h=H;

    SDL_Rect dst;
    dst.x=(int)X;
    dst.y=(int)Y;
    dst.w=W;
    dst.h=H;

    SDL_BlitSurface(surface ,&src ,window ,&dst);
}

void Item::flag(int type)
{
    TYPE = type;
}

void Item::move_to(int x,int y)
{
    X=x;
    Y=y;
}

int Item::getItemType()
{
    return TYPE;
}

const char* Item::getImgAddr()
{
    return imgAddr;
}

const char* Item::getName()
{
    return Name;
}

int Item::getDamage()
{
    return damage;
}

int Item::getAttackSpeed()
{
    return attackSpeed;
}

int Item::getX()
{
    return X;
}

int Item::getY()
{
    return Y;
}

int Item::getW()
{
    return W;
}

int Item::getH()
{
    return H;
}


Lets say i declare 2 intstances:

1
2
Item a(0,0,32,32,"name","adress to image location for the item");
Item b(54,65,32,32,"name","adress");


I tried doing a=b , but that simply doesn't do anything.
Any way i can do that?

Also the class ITEM is just another class containing data about the item(the name,the addres, the item type etc.)I currently use that to equalize 2 instances and it's quite buggy.
You can do so! See http://www.cplusplus.com/articles/y8hv0pDG/ for how C++ implicit assignment operator works.

In your example you should define your own assignment operator, to handle assignment of your three pointer attributes of your objects. Two of them, namely imgAddr and Name should be defined as std::string instead of char* to avoid a lot of pointer confusions in your objects. surface seems to point to a dynamically allocated object. Who is the owner? Who is responsible for deallocation? If a.surface should be deallocated after receiving it by SDL_DisplayFormatAlpha(), you should do so during your example assignment, you've to implement it in your operator=. (And you should do also in your destructor!) And you should think about duplicating b.surface on your intended assignment. A better way to get around all this memory allocation stuff would be using smart pointers as defined f.e. in the boost library.
Topic archived. No new replies allowed.