Assignment Operator for class with struct pointer member variable

I am getting a bad access error when the code goes into the assignment operator. Not exactly sure how to write the struct pointer member variable in terms of src.
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
#include <iostream>
using namespace std;

struct Book
{
    string title;
    string author;
};

class CSNerd
{
public:
    CSNerd(string name) {
        m_myBook = nullptr;
        m_myName = name;
        cout << name << endl;
    }
    void giveBook(string t, string a) {
        m_myBook = new Book;
        m_myBook->title = t;
        m_myBook->author = a;
    }
    ~CSNerd() {
        delete m_myBook;
    }
 
    CSNerd &operator = (const CSNerd &src)
    {
        if (&src == this) {
            return *this;
        }
        delete m_myBook;
        m_myName = src.m_myName;
        m_myBook = new Book(*src.m_myBook);
        m_myBook->title = src.m_myBook->title;
        m_myBook->author = src.m_myBook->author;
        return (*this);
    }

private:
    Book *m_myBook;
    string m_myName;
};


int main() {
    CSNerd ann("Hero");
    CSNerd ben("Zero");
    ben = ann;
    
    
} code you need help with here.
Last edited on
m_myBook = new Book(*src.m_myBook);

src.m_myBook is a null pointer; you're dereferencing a null pointer. This is a very bad thing. If you're going to dereference a pointer, make sure it's pointing at the right thing.


1
2
3
4
5
   CSNerd(string name) {
        m_myBook = nullptr;
        m_myName = name;
        cout << name << endl;
    }

When you create a CSNerd object, m_myBook is a null pointer. What's it meant to be pointing at?
Last edited on
closed account (SECMoG1T)
@Repeater told you the truth, so av tried to rewrite your op while making sure your invariants are upheld.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

struct Book
{
    string title;
    string author;

    Book()=default;//added this two to make work easier.
    Book(const std::string& _title,const std::string& _auth):title(_title),author(_auth){}
};

CSNerd &operator = (const CSNerd& src)
    {
        if(this != &src)
        {
            m_myName = src.m_myName;

            if(m_myBook)///i'd prefer smart pointers over raw ones.
                {delete m_myBook; m_myBook=nullptr;}

            if(src.m_myBook)///only access src.m_myBook->{element} when the pointer holds address to a valid object, otherwise it will e undefined behavior. 
                m_myBook = new Book(src.m_myBook->author,src.m_myBook->title);
        }
            return *this;
    }
Last edited on
These are ok:
1
2
3
4
5
6
7
8
9
10
11
CSNerd::CSNerd( string name )
  // but I prefer initiliazation
 : m_myBook(nullptr), m_myName(name)
{
  cout << name << endl; // Why this side-effect?
}

CSNerd::~CSNerd()
{
  delete m_myBook;
}


Giving you more than one book is a leak:
1
2
3
4
5
6
7
void CSNerd::giveBook( string t, string a )
{
   if ( !m_myBook ) m_myBook = new Book;

   m_myBook->title = t;
   m_myBook->author = a;
}


The copy assignment operator should follow the same theme:
1
2
3
4
5
6
7
8
9
10
11
12
CSNerd& CSNerd::operator= (const CSNerd &src)
{
  if (&src == this) return *this;

  if ( !m_myBook ) m_myBook = new Book;
  m_myBook->title = src.m_myBook->title;
  m_myBook->author = src.m_myBook->author;

  m_myName = src.m_myName;

  return (*this);
}


You do need a copy constructor too. (The Rule of Zero/Three/Five.)
Topic archived. No new replies allowed.