0

I cannot clearly state my problem in the title but this is really it.

void DChatbox::ClampObject(DTextbox _txtbox) {

    this->_txtbox = &_txtbox;

}

this one just create a copy of _txtbox not referencing it.

but this one works.

void DChatbox::ClampObject(DTextbox* _txtbox) {

    this->_txtbox = _txtbox

}

as I examined the first one, it just makes a copy of it, not referencing it. Why is it like that?

note: _txtbox on DChatbox is declared as this DTextbox* _txtbox

5
  • DTextbox _txtbox that should be DTextbox &_txtbox.pointers and reference are different. Commented Feb 3, 2013 at 13:38
  • @Arpit: I think you mean DTextbox _txtbox should be DTextbox &_textbox, don't you? The second example is correct, while the first assigns the memory address of a local variable (which is on the stack). Commented Feb 3, 2013 at 13:40
  • 2
    Be careful, the first version won't work because you are taking the address of a local variable that will be deleted when the function ends. So when the function ends, this->_txtbox will not point to a valid Dtextbox object Commented Feb 3, 2013 at 13:40
  • @Arpit where should I put DTextbox &_txtbox? in the class declaration or in the function parameter? Commented Feb 3, 2013 at 14:08
  • @Arpit I think I did it already but seems not to work? Commented Feb 3, 2013 at 14:28

2 Answers 2

2

In your first case, the pointer member gets assigned the address of an DTextbox object which is local to the function and is guaranteed to live only till the function scope{ } ends.
When you refer this->_txtbox outside the function DChatbox::ClampObject what you get is Undefined Behavior.

In second case the pointer member gets assigned to the address of object which is passed to the function and probably(difficult to say unless you show the code which calls it) the lifetime of that object being passed to the function is long enough for your program to work correctly and hence it works correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

it is called something like this: //initialization of DTextbox instance here...then mchatbox.ClamObject(&txtbox); and I get it now. Thanks!
1

That is because the first version passes the argument by value, i.e. it makes a copy of the whole object. When the method is done it simply deletes the copy. Therefore the address of that local object is invalid when the method is done, since it points to a deleted object.

The second version passes the argument using a pointer, so the pointer references the real object and not a copy of the object. Therefore, when the method is done, the value of the pointer is still the same as it points to an object that still exists.

2 Comments

I think it is not deleted because I still can see that object in my program when I run it?
@mr5: It is Undefined Behavior. The local object is only guaranteed to be alive within the function. Beyond the function the address of that object may or may not point to a valid object. Nothing is guaranteed. So it may work sometimes and may not some other times.In short it is not the behavior you should rely on, simply avoid it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.