C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-26-2009, 02:34 PM   #1
Professional Chef
 
leeor_net's Avatar
 
Join Date: Apr 2004
Location: Charles Town, WV
Posts: 144
Assignment Operator Access Violation

I've looked around a bit on Google and at a few different articles but I'm pretty sure I'm not looking for the right information.

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;
}
As a note, myBar is an object that is constructed outside of MyClass and is

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;
But that only results in the error message "cannot convert from Bar *const *' to Bar*'". I'm pretty sure const casting is not applicable here and I'm pretty sure that my first thought is just plain wrong.

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   Reply With Quote
Old 11-26-2009, 02:55 PM   #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   Reply With Quote
Old 11-26-2009, 02:55 PM   #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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 11-26-2009, 02:59 PM   #4
Professional Chef
 
leeor_net's Avatar
 
Join Date: Apr 2004
Location: Charles Town, WV
Posts: 144
Quote:
Originally Posted by anon View Post
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.
In this particular code snippet, no, I don't need to override the assignment operator. I tried to keep it as simple as possible with only the things that are relevant. The class that I actually have defined is far more complex and as car as I can tell the problem lies entirely with my pointer assignment.

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   Reply With Quote
Old 11-26-2009, 03:00 PM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-26-2009, 03:12 PM   #6
Professional Chef
 
leeor_net's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:03 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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