-
Move Semantics Techniques Program Error
Hello all expect C++ programmer, i have read an article from past 10 minutes solution but when i compiled it, it should show some error where i cannot solve it.
What is the purpose, function and benefits of move semantics ?
Error 1 error C2659: '=' : function as left operand d:\c++\move semantics\move semantics\move semantics.cpp 32
Error 2 error C2659: '=' : function as left operand d:\c++\move semantics\move semantics\move semantics.cpp 33
Error 3 error C2659: '=' : function as left operand d:\c++\move semantics\move semantics\move semantics.cpp 35
Error 4 error C2659: '=' : function as left operand d:\c++\move semantics\move semantics\move semantics.cpp 36
My program as below :
Code:
#include<iostream>
#include<string>
#include<algorithm>
#include<exception>
using namespace std;
// ---------------------------------------
void swapstr(string &, string &);
// ---------------------------------------
int main(int argc, char *argv[])
{
string first("Hellow World");
string second("C++");
swapstr(first, second);
return 0;
}
// ----------------------------------------
void swapstr(string &first, string &second)
{
size_t size;
const char *temp;
size = first.size();
temp = first.data();
// Pointer assignment not buffer copying
first.size = second.size();
first.data = second.data();
second.size = size;
second.data = temp;
}
// ---------------------------------------
http://www.devx.com/cplus/10MinuteSo...34577/0/page/2
Any help is greatly appreciated by me and others.
Thanks for your help.
Last edited by Peter_APIIT; 03-22-2008 at 03:51 AM.
-
First, you need to make sure that your code uses exactly the same statements. Particularly, initialize const variables instead of assigning to them later:
void swapstr(string &a, string &b)
{
size_t tempsz=a.size();
const char *p= a.data(); //must be initialized here
The second thing to remember is that the code sample in the solution is for illustration purposes; it's not code that you as a user can compile and run. One problem that you will face is that many string implementation don't have a data() member function. Secondly, the data member may have different names and it's usually private. So the code was meant to show how more or less move semantics can be applied to strings, not how you can rewrite std::string.
Danny Kalev
-
I still not understand what u mentioned in the previous post. Why is must be initialize rather than assigning ?
Does std::string has this data() member function ?
What is move semantics ?
What is the purpose, function and benefits of move semantics
A billion thanks for your help.
-
const objects must always be initialized. You can't assign to them later (that's why they are const in the first place!)
std::string does have a data() member function.
Move semantics and its purpose are explained in the article. In short, it's a substitute for copying, and it's much more efficient than copying.
Danny Kalev
-
Thanks for your help.
Let me relate this with real world situation.
For example, A and B house has 24 and 28 plate number respectively.
Rather than change its content of the house,w e change the number of house.
Please correct me if i wrong.
-
Yes, or a more concrete example:
cut and paste in Windows. When you move a file from one location to another, you don't create a new file. Instead, you change the directory to which the file belongs, without copying the file's data. Copy and paste uses copy semantics which means that the data is copied from the original file to a new file. You end up having two files with duplcate content.
Another example: moving a thread on this forum to another forum, as opposed to copying a thread to another forum.
Danny Kalev
-
A billion thanks for your help.
How about the program i posted before ? How to solve it ? Any hint is more than enough.
Thanks again.
-
You need to write your own string class to implement move semantics. The standard std::string class already uses move semantics under the hood, so there's no need to write special code if you use std::string:
std::string a1="hello";
std::string s2;
a2.swap(a1); //move semantics used here
Danny Kalev
-
OK.
Thanks for your help.
How about i derived a myclass from class vector and its data type is string, then i add my move semantics programming techniques to this myclass.
-
Normally, vectors use move semantics anyway when you swap two vectors.
You can't derive safely from string or vector because they don't have any virtual member functions.
Danny Kalev
-
Therefore, i need to rewrite my own class.
I don't want rewrite because it is wasting time.
My lecture taught me before copy semantics which is as below :
string s1, s2, temp;
temp = s1
s1 = s2;
s2 = temp;
This take me few hours to study this concept during my first semester.
I just want to swap two character with pointer. How to i accomplish this ?
Thanks for your help.
-
What you have is not quite move semantics but a regular swap operation. You can't tell just by looking at the syntax if a swap operation uses move semantics of copy semantics. You need to poke into the code of the string class to see how it implements its copy constructor and assignment operator to tell that.
Luckily, the string objects *do* exchange pointers when you assign to them, so in effect, swapping two strings uses move semantics. However, swapping two tm structs for example uses copy semantics.
There is a simpler form of swapping two strings:
string s1, s2;
s1.swap(s2); //exchange the two strings using move semantics
Again, you need to look into the string class to know what's happening here -- is it a move operation or a copy operation.
Last edited by Danny; 04-19-2008 at 09:08 PM.
Danny Kalev
-
You can't tell just by looking at the syntax if a swap operation uses move semantics of copy semantics. You need to poke into the code of the string class to see how it implements its copy constructor and assignment operator to tell that
Why move semantics related to copy constructor and assignment operator ?
How about if i write a link list that use a move semantics techniques ?
I just want to learn move semantics.
A billion thanks for your help.
Danny is C++ god.
-
Because the copy ctor and the assignment operators are the only standard means of moving/copying objects' states. How else would you move an object to another object? You can define a member function called swap(), or move() but your users will have to look at the class declaration to know how to use these functions. Besides, the copy ctor and assignment op are generated by default, and they offer copy semantics by default. If you want to have move semantics, you have to override the compiler-generated implementation with your own move-based implementation.
As for linked lists: again, it all boils down to how you implement the list's copy ctor and assignment operator. You need to make these member functions exchange pointers, basically.
Danny Kalev
-
How to exchange pointer ?
Sorry for my stupidity.
Similar Threads
-
By zobi316 in forum VB Classic
Replies: 3
Last Post: 03-10-2008, 07:05 AM
-
By amitid4forum in forum Java
Replies: 0
Last Post: 11-24-2007, 06:34 AM
-
By Chris H Baker in forum C++
Replies: 5
Last Post: 01-17-2007, 01:37 PM
-
By Murray Foxcroft in forum Web
Replies: 5
Last Post: 11-02-2000, 02:42 AM
-
By mrazek in forum VB Classic
Replies: 0
Last Post: 05-22-2000, 08:34 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks