I am reading this book called "C++ From the Ground Up".

I saw this and it makes perfect sense

// Using a reference parameter.
#include <iostream>
using namespace std;
void f(int &i);
int main()
{
int val = 1;
cout << "Old value for val: " << val << '\n';
f(val); // pass address of val to f()
cout << "New value for val: " << val << '\n';
return 0;
}
void f(int &i)
{
i = 10; // this modifies calling argument
}


This program displays the following output:
Old value for val: 1
New value for val: 10
Pay special attention to the definition of f( ), shown here:
void f(int &i)
{
i = 10; // this modifies calling argument
}
Notice the declaration of i. It is preceded by an &, which causes it to become a
reference parameter. (This declaration is also used in the function’s prototype.)
Inside the function, the following statement
i = 10;
does not cause i to be given the value 10. Instead, it causes the variable referenced by
i (in this case, val) to be assigned the value 10. Notice that this statement does not
use the * pointer operator. When you use a reference parameter, the C++ compiler
automatically knows that it is an address (i.e., a pointer) and de-references it for you.
In fact, using the * would be an error.
I understand that above program completely but then i see this other program about "Using a copy constructor to construct a parameter" and it just confuses the hell out of me.

Here is a section of the code

1 myclass::myclass(const myclass &obj)
2 {
3 p = new int;
4 *p = *obj.p; // copy value
5 cout << "Copy constructor called.\n";
6 }

Why is the * operator used in obj in line 4? Shouldn't that be an error? I am so confused. I am just thinking shouldn't it be automatically derefenced for you?

Recommended Answers

All 7 Replies

Oh i think i get it. It is referening to the copy not the original. I think. Probably got no idea.

because 'p' is a pointer i.e holding an address to a type, variable or object.
since : p = new int;
therefore p is holding the memory of an int

the preceding '*' is used to de-reference the pointer...
that is: "I don't know what *obj.p is, but assign whatever value is in *obj.p to *p .
memory....!
I hope you understand!

nope... you de-reference, when you want to work with the value obj.p its pointing to...

I am not really getting it but thanks for the help. I am just going to go back a couple of chapters and review pointers, and references as parameters but it doesn't say much about that, just one little page, the one I copied and pasted here.

The dereference operator (*) doesn't cause an error because it is simply dereferencing the actual value. It is the same as altering the value of a variable passed by reference, however since it's done by pointer, to assign/reassign the value of a pointer we need to dereference it so we're changing the VALUE of the pointer and not the ADDRESS (&) of the pointer. Does that help at all?

Read this: but specifically, Go to the C++ Section
http://cslibrary.stanford.edu/106/

void main() {   
    int*    x;  // Allocate the pointers x and y
    int*    y;  // (but not the pointees)

    x = new int;    // Allocate an int pointee,
                    // and set x to point to it

    *x = 42;    // Dereference x to store 42 in its pointee

    *y = 13;    // CRASH -- y does not have a pointee yet

    y = x;      // Pointer assignment sets y to point to x's pointee

    *y = 13;    // Dereference y to store 13 in its (shared) pointee
}

Actually, your constructor is very badly written - I give it an 'F'. More properly it should be like this:

myclass::myclass(const myclass &obj)
: p(new int)
{
 *p = *(obj.p); // copy value
 cout << "Copy constructor called.\n";
}

Note the instantiation of the pointer member in the initialization block before the body of the constructor itself. Also, note the parens scoping the copy and member p when dereferencing it. If you said *p = *obj.p; you have to ask yourself whether the *obj.p means dereference obj, or dererence the p member of obj. If for some reason obj was a pointer, then it would have meant (*obj).p which would result in the address of p, not the value contents of p.

My point in all of this is that you need to be very careful in C++ to code EXACTLY what you mean and intend. Don't make assumptions, but understand precisely what you mean. C++ is a very precise language, and failure to incorporate that precision in your code is just asking for a lot of really nasty bugs to reside therein.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.