c++ - Learning OOP: Overloading the + or = operator error -


i thought edit else sees it. have fixed answers of guys. error because of having delete pointer in both constructors stupid reason lol. removed them both , compiles fine :d

hi have been working extensively past few days learning more on classes , operator overloading, copy constructors etc. using ivor hortons visual c++ 2012.

i have created class same concept time tried make code clearer output showing objects created when , happens whenever, pretty experimenting getting used flow of execution , not.

now in class made, keep getting access violation after adding operator overload "+" , "=" in class. have looked @ previous classes have wrote , cannot see how have done different time. code have been working with:

#include <iostream> #include <string>  using namespace std;  class game { public: static int countobj;  explicit game(char* title = "default title", int difficulty = 10, int players = 4) {     delete [] mp_title;     mp_title = new char[strlen(title) + 1];     strcpy_s(mp_title, strlen(title) + 1, title);     m_difficulty = difficulty;     m_players = players;     m_objid = countobj;     cout << "object " << ++countobj << " created" << endl; }  game(const game& objgame) {     delete [] mp_title;     mp_title = new char[strlen(objgame.mp_title) + 1];     strcpy_s(mp_title, strlen(objgame.mp_title) + 1, objgame.mp_title);     m_difficulty = objgame.m_difficulty;     m_players = objgame.m_players;     cout << "object " << ++countobj << " created using copy constructor" << endl;     m_objid = countobj;  }  ~game() {     cout << "destructor called on object: " << this->getobjid() << endl;     delete [] mp_title; }  game operator=(const game&& objgame) {     cout << "= operator used rh expression on object: " << this->getobjid() << endl;     delete [] mp_title;     mp_title = new char[strlen(objgame.mp_title) + 1];     strcpy_s(this->mp_title, strlen(objgame.mp_title) + 1, objgame.mp_title);      return *this; }  game& operator=(const game& objgame) {     if (this != &objgame)     {         cout << "= operator called on object " << this->getobjid() << endl;         delete [] mp_title;         size_t len = strlen(objgame.mp_title) + 1;         mp_title = new char[len];         strcpy_s(mp_title, len, objgame.mp_title);         m_difficulty = objgame.m_difficulty;         m_players = objgame.m_players;     }      return *this; }  game operator+(const game& objgame) const  {     cout << "+ called" << endl;     return game("default title",                 (m_difficulty + objgame.m_difficulty),                 (m_players + objgame.m_players) ); }   char* gettitle() { return mp_title; } void settitle(char* title) { mp_title = title; } int getobjid() { return m_objid; }  private: char* mp_title; int m_difficulty; int m_players; int m_objid; };  int game::countobj = 0;  int main() {  game game1("game 1", 10, 4); game game2("game 2", 5, 2); game game3 = game2;  game2.operator=(game3.operator+(game1));  cout << game1.getobjid() << endl << game2.getobjid() << endl; cout << game1.gettitle() << endl << game2.gettitle() << endl;  return 0; 

}

so said, the:

game2 = game3 + game1; 

because when removed program runs. feedback on coding styles , conventions use in code. in advance.

the error in debugger is: 0xc0000005: access violation reading location 0xccccccc0

delete [] mp_title; 

this deletes @ point uninitialized pointer mp_title. never need in constructor (as definition uninitialized). goes both constructors, no others.

as generic hint, c0000005 segmentation fault (access of kind of memory isn't allocated, , addresses starting ccccc or cdcdcd visual studio's default debug filler uninitialized memory.

game operator=(const game&& objgame) 

this 1 can swap current , other's contents; you're guaranteed argument destructed next can use "waste disposal". no need make copy, swap.

    size_t len = strlen(objgame.mp_title) + 1;     mp_title = new char[len];     strcpy_s(mp_title, len, objgame.mp_title); 

this microsoft-ism. strcpy_s not quite portable strcpy (1) , in case, you're passing strlen(arg3) length anyway, making not hair safer using strcpy (2). used using strcpy , find out when unsafe; prevent more bugs blanket use of strcpy_s without knowing bugs you're trying prevent.

just tried running on gcc 4.7.3 (default ubuntu 13.04 compiler) , after removing said 2 deletes constructors, runs fine. result is

object 1 created object 2 created object 3 created using copy constructor + called object 4 created = operator used rh expression on object: 1 object 5 created using copy constructor destructor called on object: 5 destructor called on object: 3 0 1 game 1 default title destructor called on object: 3 destructor called on object: 1 destructor called on object: 0 

which looks bit wrong (especially object ids), doesn't crash.

your settitle wrong (doesn't copy argument) you're not using that's not causing trouble now.

and people have mentioned before, should using std::strings prevent kind of messups. in fact, if used remove of constructors & destructors , let default-generated ones work you.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -