If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: my own property code

Originally Posted by VictorN

1. What did you expect and what did you get?
2. Did you debug your code?

i fix that...
the problem(see just the main() code) was that i used instance 'b' but changes on instance 'a'(like 'b' be aponted to 'a').
i did a Copy Construtctor, in the test class, for avoid copy the 'this', when we do:

something that i have learned yesterdar, before i lose the electricity, because of the whether(rain)
the problem wasn't my property code, that needs the 'this' pointer class... but in test class, because, by defauld, the compiler do a Copy Constructor(if we don't do it) and copy the 'this' too

Re: my own property code

Originally Posted by Cambalinho

i fix that...
the problem(see just the main() code) was that i used instance 'b' but changes on instance 'a'(like 'b' be aponted to 'a').
i did a Copy Construtctor, in the test class, for avoid copy the 'this',

Well, you've now made your class almost useless in a real program. You've now introduced side-effects in your copy-assignment operations.

The purpose of the copy constructor (and assignment operator, which you didn't implement) is to do just that -- make copies. If you start to make fake copies of your objects using the copy-assignment operator, that opens the code up for a lot of runtime bugs that are insanely difficult to solve.

In other words, what if I tried to create a vector<test> and started to manipulate this vector by resizing it, removing items, etc.? What will happen? Chaos.

When creating a copy constructor and assignment operator, the goal is to make sure that the copied object behave exactly the same as the original object. In other words, if I take that copy and use it instead of the original object, the resulting program must behave exactly the same externally. For example:

What if the implementors of std::string decided to add an extra character (maybe a blank) to s2 when a copy is made, thus making the length f s2 equal to 4 instead of 3? Then std::string would be totally unusable in any program except a toy one.

Your property class itself implements the copy constructor with side-effects. Why didn't you copy the std::function members? You only copied the PropertyValue. So already you're in trouble.

If you need to change the object to "almost copy" another object, then provide that function -- do not use the copy constructor or assignment operator for this purpose.

Re: my own property code

Originally Posted by Paul McKenzie

Well, you've now made your class almost useless in a real program. You've now introduced side-effects in your copy-assignment operations.

The purpose of the copy constructor (and assignment operator, which you didn't implement) is to do just that -- make copies. If you start to make fake copies of your objects using the copy-assignment operator, that opens the code up for a lot of runtime bugs that are insanely difficult to solve.

In other words, what if I tried to create a vector<test> and started to manipulate this vector by resizing it, removing items, etc.? What will happen? Chaos.

When creating a copy constructor and assignment operator, the goal is to make sure that the copied objects acts exactly the same as the original object. In other words, if I take that copy and use it instead of the original object, the code must behave exactly the same externally.

Your property class itself implements the copy constructor with side-effects. Why didn't you copy the std::function members? You only copied the PropertyValue. So already you're in trouble.

If you need to change the object to "almost copy" another object, then provide that function -- do not use the copy constructor or assignment operator for this purpose.

Regards,

Paul McKenzie

"Why didn't you copy the std::function members?"
because they are dependent of 'this' class... so how can i avoid that?

Re: my own property code

Originally Posted by Cambalinho

"Why didn't you copy the std::function members?"
because they are dependent of 'this' class... so how can i avoid that?

That's your problem to solve.

What I'm saying is that you are not to use the copy-assignment operator to do this. You are introducing side-effects in an operation that you have no exact control over when or where it will be used (the compiler can also call the copy constructor and/or assignment operator "behind your back").

Again, look at my vector example. You can't use your property class in a vector safely (or any other container that makes copies).

I know you're testing with toy programs, but you can't just look at your little program before making these decisions. You must look at the big picture. Realizing that your classes can't be used safely in containers should have been on your list of items you should have been aware of.

Re: my own property code

Originally Posted by Cambalinho

so how can i avoid that?

This shows that writing proper C++ classes takes a lot more thought than just picking up syntax here and there and throwing together a class that happens to compile. Your design is flawed, plain and simple.

So that is your homework -- come up with a design that doesn't violate the rules of creating valid copies, but allows the user to "clone" another object. Yes, it takes thought (on paper, using UML, whatever), before writing any code. Once that's OK, then you implement the C++ class.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.