Jump to content
  • Advertisement
Sign in to follow this  
nlbs

constructor taking const

This topic is 3639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my code
Js::JVar::JVar(const JVar& r): Var(), rhs(auto_ptr<Var>(&r)){

}

auto_ptr<Var> rhs;
I know here rhs is dtoring non-const objects and I am specifying a const. which makes the compilation error. But how to handle the situation Now ?? using Just Js::JVar::JVar(JVar&) is not a good way cause the original source will be changed and somebody will think that its unexpected. and I am not sure about how good it is to use Js::JVar::JVar(JVar)

Share this post


Link to post
Share on other sites
Advertisement
Why are you stuffing r into an auto_ptr? How do you know the object passed in was allocated on the free store? It may have been allocated on the stack.

If you want to transfer ownership to an auto_ptr member, your constructor should take an auto_ptr<>& as an argument

Share this post


Link to post
Share on other sites
people might code

x = 2;
x = "Hello";

so I believe when somebody does x="Hello" the Object created for storing 2 will be automatically deleted Right ??

But If I use general pointers it would not.

am I wrong ?

Share this post


Link to post
Share on other sites
For starters, the copy constructor isn't called if you're not constructing the object. Something like this:

Object foo;
Object bar = foo;

Would call the copy constructor on 'bar'. However this:

Object foo;
Object bar;
bar = foo;

Would invoke the assignment operator (ignoring any potential compiler optimizations).

The object should be assumed to be in an undefined state in the constructor, so you don't have to worry about any existing pointers being deleted or other resources being released, since when you're in the constructor the object is existing for the first time so there are no existing resources to worry about.

RDragon1's point is that an 'auto_ptr' is used to automatically delete a pointer when the object goes out of scope. However you have no guarantee that the object passed into your copy constructor was allocated with 'new', so what you're doing is Very Bad. If someone passed in a stack object (and it even looks that way in your example), the application would crash/assert when 'JVar' goes out of scope.

What are you trying to achieve exactly? Proper RAII?

Share this post


Link to post
Share on other sites
What you've mentioned are well known basics.
The problem is not the constructor I can do something on operator=() overload too.

Suppose I've a Class called Foo, now:

Foo x;
x = 2;
x = "Hello"
x = new Bar();
x = new Stick(5.0);
y = new Bar();
x = y;


whenever operator=(SOME BASIC TYPE) will be used an equivalent new CustomInt(i) or new CustomString(str) will be created and there is a chance to reassign an object again and again so If I use general pointers all these anonymous objects will still stay in heap.

To do this with general pointers I need to use
delete rhs
or similar code at the begening of the rhs setter functions.but by doing that everytime the previous one will be deleted e.g. in case of y also it will happen.

and thats why I was interested about std::auto_ptr so that these anonymous objects can be deleted automatically.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!