Skip to main content
GameDev.net gamedev.net
🔒 Locked

this pointer question....

Started by mits Mar 8, 2004 at 4:14 PM 7 replies 450+ views
Original Post
mits
mits
I have a vector class: class cvector { protected: float m_fvec[3]; public: cvector(cvector& p_vec) { *this = p_vec; // Line 1 } }; Program compiles well but when running it throws an exception in line 1. What''s wrong? Thanks.
Agony
Agony
I'm not sure, since I haven't tested it, but my guess is that you're getting a stack overflow exception, because the = operator actually calls the copy constructor, so you have infinite recursion. The constructor keeps calling itself. You'll have to manually copy member variables. However, if you don't specifically define a copy constructor, a default one is made for you, and all it does is copy the member variables, byte-by-byte. For non-complex classes with no pointers, this should be perfectly fine.

[Edit - Okay, apparently I was wrong. A default operator =() function is created, which performs operator =() on every single member, which is indeed a different function from the copy constructor. I'm outta help. Sorry.]


int main() { return *((int*)0); }

[edited by - Agony on March 8, 2004 5:29:37 PM]
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
SiCrane
SiCrane
What kind of exception is it throwing and how are you invoking the copy constructor?
pjcast
pjcast
[Edit]I was wrong...[/Edit]But,
Actually, I take my previous statement back

if you show some code - how you call the cc
Also, try overloading the assignment operator yourself
And the error message would help too.

[edited by - pjcast on March 8, 2004 5:38:44 PM]

[edited by - pjcast on March 8, 2004 5:50:23 PM]
Fruny
Fruny
quote:
Original post by pjcast
[Disclaimer]I could be wrong...[/Disclaimer]But,
I don''t think that the "this" object is not fully constructed until after the constructor is called. And your using it and dereferencing some undefined place in mem. Therefore causing an exception. No need to use the this pointer.



It''s true to some extent for the initializer list.
Once you''re in the code block, you''re fine.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Spudder
Spudder
I''m guessing a memory access violation through trying to assign a value to a memory address (the dereferenced this pointer), change line one to something like this: memcpy(m_fVec, &p_Vec, (sizeof(float) * 3))
daerid
daerid
It''s kind of a useless constructor, since you don''t have an overloaded = operator


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
amag
amag
quote:

It''s true to some extent for the initializer list.
Once you''re in the code block, you''re fine.

Actually, an object is not safely created until the ctor exits normally. If the ctor throws, then the object was never constructed. However the memory for an object is allocated before the ctor (and initialiser list) runs.
Fruny
Fruny
quote:
Original post by amag
Actually, an object is not safely created until the ctor exits normally. If the ctor throws, then the object was never constructed. However the memory for an object is allocated before the ctor (and initialiser list) runs.


If the constructor throws, the destructor will indeed not be run since the object was never fully constructed. Subobject will, however, be destroyed normally (to the extent they were constructed in the first place).

However, within the constructor block, you can refer to this without any problem, which was the point of my post.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.