![]() |
| | #1 |
| Professional Chef Join Date: Apr 2004 Location: Charles Town, WV
Posts: 144
| Assignment Operator Access Violation In the following scenario, I have a very basic assignment operator: Code: class MyClass
{
public:
MyClass(Bar *bar) : myBar(bar) {};
MyClass(): myBar(NULL) {};
MyClass& operator=(const MyClass &rhs)
Bar *myBar;
};
MyClass& MyClass::operator=(const MyClass &rhs)
{
myBar= rhs.myBar; // explodes here, access violation at 0x00000000
}
int main()
{
Bar *someBar = new Bar();
MyClass *myClass = new MyClass(someBar);
MyClass *myClass2 = new MyClass();
*myClass2 = *myClass;
return 0;
}
What I expect is that the address of rhs.myBar be assigned to myBar. However, I get an access violation attempting to write to memory 0x00000000 which tells me that I'm trying to write into a null pointer. My first thought was that I should be asking for the address of rhs.myBar like this: Code: myBar = &rhs.myBar; Obviously my code is incorrect. What am I doing wrong here? Where can I find more information about the particular type of operation I'm trying to do?
__________________ - Leeor Last edited by leeor_net; 11-26-2009 at 02:54 PM. Reason: Bit of clarification. |
| leeor_net is offline | |
| | #2 |
| Robot Join Date: Mar 2009
Posts: 219
| Does this work? Code: MyClass& MyClass::operator=(const MyClass &rhs)
{
if (this == &rhs)
{
return *this;
}
myBar= rhs.myBar;
return *this;
}
|
| Memloop is offline | |
| | #3 | |
| The larch Join Date: May 2006
Posts: 3,175
| I doubt the error is in that code (you are not dereferencing the pointer anywhere). You shouldn't even need to define the assignment operator yourself.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #4 | |
| Professional Chef Join Date: Apr 2004 Location: Charles Town, WV
Posts: 144
| Quote:
As for the first reply, that is not the issue either as I know for sure that I'm not looking at the same object (different heap addresses) although I have a feeling that'll help to prevent silly errors later on.
__________________ - Leeor | |
| leeor_net is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,463
| Aside from missing ; and missing return statement (and the missing delete statements!), the code compiles and runs fine. If you encounter a problem, you would have to post the smallest possible amount of code that demonstrates the problem.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| Professional Chef Join Date: Apr 2004 Location: Charles Town, WV
Posts: 144
| As it turns out I was looking for the wrong information having misdiagnosed the problem. I didn't think my assignment above was wrong and, upon further inspection I found that I had initialized MyClass2 to NULL (e.g., SomeClass() : myClass2(NULL)) and had then attempted the assignment operator. Thank you, everyone, for your time. I always appreciate your help!
__________________ - Leeor |
| leeor_net is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Menu | Krush | C Programming | 17 | 09-01-2009 02:34 AM |
| Access violation... can't figure it out... | Raigne | C++ Programming | 7 | 10-11-2007 10:52 AM |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| Help with a pretty big C++ assignment | wakestudent988 | C++ Programming | 1 | 10-30-2006 09:46 PM |
| A mini-compilier I made stuck in an infinite loop (lots of code) | dan06 | C++ Programming | 1 | 10-27-2006 01:21 PM |