How to return itself object?

Debugger say it's access violation on runtime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MyClass &MyClass::operator = (const MyClass &myClass)
{
	a = myClass.a; // it's crash after this line
	b = myClass.b;
        return *this;
}

MyClass MyClass::getMyClass()
{
	MyClass wantedObject;
	return wantedObject;
}

MyClass a;
MyClass b;

b = a.getMyClass(); // Runtime error
// I don't want a = b; I just want another MyClass object from getMyClass to return. 
Last edited on
What is the context of line 15?

getMyClass is a member function, so it either has to refer to an object instance (which it doesn't) or be called within another member function of MyClass (which you haven't shown).

As shown, getMyClass is going to construct a default instance and return it by value.

Compilers don't detect run time access violations. A run time access violation is something that happens AFTER your program is compiled when you execute your program.
Oh. Sorry, I mean debugger.
Nothing special on MyClass

1
2
3
4
5
Myclass ()
{
   int a;
   int b;
}
Topic archived. No new replies allowed.