why reference ?

E

E. Robert Tisdale

William said:
References introduced make things much more complicated. e.g.

int a;
void f(int &n);
void g(int *p);

Don't do that!

Pass by value, const reference or const pointer
and return by value:

int f(const int& n) {
return n + 1;
}

int g(const int* p) {
return *p + 1;
}
 
W

William Xuuu

John Harrison said:
Try writing an assignment operator using a pointer.

class X
{
X& operator=(const X* rhs);
};

Then you would then have to write

X x, y;
x = &y;

Do you think that is good style?

Hmm, i see your point. But the assignment operator is different from a
general method, we don't generally call it as x.operator=(&y). Meanwhile in
my e.g., i'd say g(&a) is better than f(a), right? So, there are some
things in c++ that reference performs more elegant than pointer, not
all. That's all.
References and pointers are different. The main difference is that a
reference once initialised cannot be made to refer to something else. This
is often exactly what you want, and makes using references very common, it
also makes the syntax simpler.

One has to master two things now, compared with one before. I can't agree.
 
C

Cheng Mo

Hi,
I am new to C++.
In Java, it is possible to define one class to be "final" so no other
class can inherit it.
I am wondering whether or not there is some mechanism in C++ do the same
job. Besides putting all constructors privte, any other solution?
 
J

John Harrison

One has to master two things now, compared with one before. I can't agree.

Fair enough, you're free not to use the parts of C++ that don't appeal to
you. You can just use them for parameters to overloaded operators and copy
constructors (where they are essential).

It took me along while to get used to references but now I use them all the
time.

john
 
U

Ulf Wikstr?m

William Xuuu said:
Hi,

References introduced make things much more complicated. e.g.

int a;
void f(int &n);
void g(int *p);

Isn't g(&a) much more clear than f(a), when we would change the value of
the lvalue a ?

Are there any efficiency differences ?

Is reference a must in c++ ?

thanks.

The reason that Stroustup introduced references into C++ was actually
operator overloading.
For example, in the assignment operator, the return type must be a
reference.
 
B

Branimir Maksimovic

Cheng Mo said:
Hi,
I am new to C++.
In Java, it is possible to define one class to be "final" so no other
class can inherit it.
I am wondering whether or not there is some mechanism in C++ do the same
job. Besides putting all constructors privte, any other solution?

// put this in header file:
// header_file.h
// -----------------------------
#ifndef HEADER_FILE_H
#define HEADER_FILE_H
class BaseOfFinalClass{
public:
// ...some interface functions eg
virtual void function1()=0;
virtual ~BaseOfFinalClass(){}
};

BaseOfFinalClass* makeFinalClass();
#endif
// ------------------------------

// put your final class in implementation file:

// implementation_file.cpp
// ------------------------
#include "header_file.h"
class FinalClass: public BaseOfFinalClass{
public:
// ... implementation functions
virtual void function1(){ /* ... */ }
};

BaseOfFinalClass* makeFinalClass()
{
return new FinalClass;
}
// ------------------------

Greetings, Bane.
 
A

Arijit

Old said:
Arijit wrote:

double GetFirstNegative(double* pNums)
{
int i=0;
while(pNums>0.0)
++i;
return pNums;
}

No pointers, no references. Agreed, has an extra variable.



This still uses a pointer. pNums is a pointer. The fact that
there is no '*' symbol in the code does not change this.
pNums is the same as: *(pNums + i)


I meant no explicit pointers are used. I indeed tried to write the
program without * . I consider using [] to dereference a pointer not
using pointers. You can never eliminate implicit pointers. For example,
references are usually implemented as pointers.
Pointer free, array passed by reference

double getFirstNegative(double arr[]) {

This was not written by me.
In this case, arr is a pointer. This is identical to the
original declaration.

To pass an array by reference you need to write:
double foo( double (&arr)[10] )

Yes. But will there be any difference in usage of arr in the two cases ?

-Arijit
 
D

Default User

Cheng said:
Hi,
I am new to C++.


I think you are new to newsgroups as well, as you posted your
completely irrelevant message in reply to someone else. Learn how to
use Thunderbird to start new threads rather than piggy-back on existing
ones.





Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top