small tutor question

1
2
3
4
5
6
7
8
CExample::CExample (const CExample& rv) {
  a=rv.a;  b=rv.b;  c=rv.c;
  }

Therefore, the two following object declarations would be correct:

CExample ex (2,3);
CExample ex2 (ex);   // copy constructor (data copied from ex)[ 



In the tutorial about classes the introduction of copy instructer and copy assignment operator begins. To me this explanation was rushed big time, why even include it in the tut if nothing is explained..

Like what is CExample& , why is there a reference symbol at the end??
and what is rv?
Also, what is a, b, and c? (lol?)

Any help would be appreciated
Last edited on
You mean copy constructor?

There is a reference symbol so the constructor will not need to make a copy. This is for efficiency purposes.

rv is the reference to an object of the same class CExample. Like in the example given above:
1
2
CExample ex(2,3);    //ex is an object/instance of class CExample
CExample ex2(ex);   //ex is passed to the constructor. Calling the specific overloaded constructor which is the copy constructor. 


a, b, and c are data members of the CExample class. The above copy constructor is typically how a compiler-generated copy constructor works.
Ok I think that im getting closer to understanding this rv is data type CExample&
Its referenced too, although I dont understand why the reference symbol is at the end not beggining

I think the most confusing thing tho is that the copy constructor includes 3 data members, and when object 'ex' is declared it is with 2 data members. How are these data members passed to the class (2, 3)?

would it be something like ex2 would have a = 2, and b = 3


Besides that I think I understand now, copy constructor is simply a function that makes things faster when you want to copy another object with same type's members (correct)?
Last edited on
closed account (z05DSL3A)
Just before the section you are talking about is this:
1
2
3
4
5
6
class CExample {
  public:
    int a,b,c;
    CExample (int n, int m) { a=n; b=m; };
    void multiply () { c=a*b; };
  };


the copy constructor is added to this class, so you now have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class CExample {
  public:
    int a,b,c;
    
    CExample (int n, int m) 
    { 
         a=n; 
         b=m; 
    };
    
    CExample (const CExample& rv) 
    {
        a=rv.a;  
        b=rv.b;  
        c=rv.c;
    }

    void multiply () { c=a*b; };
  };


if you are have truble understanding passing by reference then give the functions (II) tutorial a read.
http://www.cplusplus.com/doc/tutorial/functions2.html


CExample ex(2,3); calls the constructor for the CExample class, the class has three member (a,b, and c) but the constructor only need two arguments to construct an object.
Last edited on
Ok now I could have used logic and ASSUMED everything you just typed greywold, but i prefer to have 100% knowledge. I figured that a,b,c objects were declared earlier, and the example was just not really complete. But you never really know how abstract c++ could be, so i didnt really know for example, if rv is the only way to declare a reference to the object. See what I mean? (although now I know rv is just an identifier of type CExample that obtains the adress of that object being passed)

Now that I've read classes (II) some things have been cleared up.
However, your example made things much more clear. (esp that this constructor is also auto called on declaration). Even tho the tutorial didnt explicitly state that, should I have assumed it?

It just sort of dissapointed me when I came across the first example where it just seemed like a half attempt, although I must admit the rest of the tutorial is the best I've seen for cpp.

btw i did read functions (II), but I did forget that part of it, thanks so much for reminding me.
Last edited on
Topic archived. No new replies allowed.