473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

function Declaration&Definition

As i'm c++ beginner the following Function Declaration and Definition couldnt understandable? so explain me what kind of Declaration this is......?
especially X& refers what?
Here instead of datatype X& is used. why?

Declaration :
private:

int len;
char *ptr;

public:

X& Set(char *);


Definition
X& X::Set(char *pc)

{
len = strlen(pc);
ptr = new char[len];
strcpy(ptr, pc);
return *this;
}

Also pleas explain *this without more complex technical terms.
Jun 21 '07 #1
1 1086
The main thing to know here is about copy constructor and references. Learn that quick if you want to improve your programming skills.

Example with builtin type (int, float, ...):

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. int j = i;
  3.  
  4. i = 2;
  5. ASSERT(j == 0);  //is true
j is a copy of i at a given time. So changing i doesn't affect j;

But, if i had a reference on i, thinkgs would have been different:

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. int & j = i;  //here j is a reference on i
  3.  
  4. i = 2;
  5. ASSERT(j == 2);  //is true

This is the same thing for complex object like class or struct. The reasons we use them more on complex object its because that prevent a copy construct called which is totally unefficient.

Expand|Select|Wrap|Line Numbers
  1. string s = "a very long and very big string ...";
  2.  
  3. string s2 = s;  //s2 is a copy so the whole string was copied
so using function like this:

Expand|Select|Wrap|Line Numbers
  1. void f(string s);  
  2. f(s);  // you have a copy passed as an argument, this is slow
But:

Expand|Select|Wrap|Line Numbers
  1. void f(string const &s);  
  2. f(s);  // very quick parameters passing. The const is used to tell the compiler that s is supposed to be unmodified in f.
because:

Expand|Select|Wrap|Line Numbers
  1. void f(string &s);  
  2. f(s);  //quick but my string s could be totally damaged in f!
So references act like pointers, but they are more elegant and intuitive to use because if you used a reference you do not have to test if it is null or not. Compiler garanty that it can't be null, though it can reference a deleted object, but that's another story.

Also about "this". this is the pointer on the class object your in. "this" refers to your current object in a given class method. So "*this" means the class object derefenrenced. So you can return yourself either as a copy (slow) or as a reference (quick) either const or not.
Jun 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: qazmlp | last post by:
// test.h class test { public : inline void check() ; } ; // test.C inline void test::check() // Is 'inline' optional here?
28
by: Michael B. | last post by:
I tend to use rather descriptive names for parameters, so the old style of declaration appeals to me, as I can keep a declaration within 80 chars: void * newKlElem...
4
by: thinktwice | last post by:
i have just made a test project :(win32 console) //file : func.h #ifndef _FUNC_H_ #define _FUNC_H_ void func1() { return; };
10
by: Eric | last post by:
Hello, I have been working through the K&R book (only chapter one so far) examples and exercises. After much sweat and hair pulling, I think I have a solution for ex 1-18 on page 31. It...
3
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
13
by: Old Wolf | last post by:
I have some code that has in the header file: void foo( char bar ); and in the source file: void foo( bar ) char bar; { /* etc. */ } The compiler (with many warnings enabled) warns that...
1
by: wtu | last post by:
function Declaration&Definition -------------------------------------------------------------------------------- " Geroty (const vector<Point3D>& poly, const Plane& Tr)" As i'm c++ beginner...
2
by: hellori | last post by:
Hi all, I have a question about passing iterators to a function. I want to create a function that does something with STL container iterators. The container of the iterator can vary, so I use a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.