473,499 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer & reference doubt!

HI,
One more small doubt from today's mail.
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference (or better a const
reference).

for eg,
const PointRange* points = cc.points(ptAligned);

cc.points is a const member function which returns a const pointer to
class PointRange.
This member function (from a different class) needs a const reference
to PointRange.
convolveNearEdge(const PointRange& points,const Kernel& kernel, ...) {}
I am passing it the reference as,
convolveNearEdge(*points,getKernel1(),velocity);

I do not need a null in the function thus a reference. also I make
assure points passed to convolveNearEdge is not null.

My question is, in the process do anywhere copy of the class occures
silently? I have no problem if it copies a pointer. But do not want a
copy of the class even temporarily.

Thus fundamentally , if I have a pointer, say int* x; *x = 10; then
int& y = *x is essentially a reference to the value pointed by x? and
without any overload?
Thanks

Sep 13 '06 #1
8 2378
toton wrote:
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference (or better a const
reference).

for eg,
const PointRange* points = cc.points(ptAligned);

cc.points is a const member function which returns a const pointer to
class PointRange.
This member function (from a different class) needs a const reference
to PointRange.
convolveNearEdge(const PointRange& points,const Kernel& kernel, ...)
{}
I am passing it the reference as,
convolveNearEdge(*points,getKernel1(),velocity);

I do not need a null in the function thus a reference. also I make
assure points passed to convolveNearEdge is not null.

My question is, in the process do anywhere copy of the class occures
silently?
Unless there is a conversion from PointRange to something inside the
'convolveNearEdge' function... No, returning a pointer does not need
to make a copy, passing a reference doesn't either.
I have no problem if it copies a pointer. But do not want a
copy of the class even temporarily.
You can always put a breakpoint in the copy constructor and see if it
gets hit, you know...
Thus fundamentally , if I have a pointer, say int* x; *x = 10; then
Ahem... That's a bad idea. 'x' points to nothing and you try to
dereference it...
int& y = *x is essentially a reference to the value pointed by x? and
without any overload?
Yes, supposedly. Using 'y' after that is just like using '*x'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #2

Victor Bazarov wrote:
toton wrote:
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference (or better a const
reference).

for eg,
const PointRange* points = cc.points(ptAligned);

cc.points is a const member function which returns a const pointer to
class PointRange.
This member function (from a different class) needs a const reference
to PointRange.
convolveNearEdge(const PointRange& points,const Kernel& kernel, ...)
{}
I am passing it the reference as,
convolveNearEdge(*points,getKernel1(),velocity);

I do not need a null in the function thus a reference. also I make
assure points passed to convolveNearEdge is not null.

My question is, in the process do anywhere copy of the class occures
silently?

Unless there is a conversion from PointRange to something inside the
'convolveNearEdge' function... No, returning a pointer does not need
to make a copy, passing a reference doesn't either.
I have no problem if it copies a pointer. But do not want a
copy of the class even temporarily.

You can always put a breakpoint in the copy constructor and see if it
gets hit, you know...
Thus fundamentally , if I have a pointer, say int* x; *x = 10; then

Ahem... That's a bad idea. 'x' points to nothing and you try to
dereference it...
Very bad .... . May be this is the only reason I never use a pointer
which is not a class member. read the code as . int* x = new int; *x =
10; int& y = *x;
and at last delete x;
int& y = *x is essentially a reference to the value pointed by x? and
without any overload?

Yes, supposedly. Using 'y' after that is just like using '*x'.

V
Thus i feel that a pointer & reference can be freely interchangable
without any overhead provided the pointer is pointing to a valid
object.
I usually pass a const / non const reference as it always hold a valid
object & easier syntax. However I prefer return as pointer over
reference (const / non const) , when I am not supposed to return value,
because sometimes I forget to work with the reference, and use a copy
instead, which do not serve the purpose of returning reference.
like instead of writing
test11& t1 = t2.t();
occationally end up with
test11 t1 = t2.t();
:(
Any suggestion regurding this problem?

Thanks
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #3

Victor Bazarov wrote:
toton wrote:
I have certain function which returns a pointer (sometimes a const
pointer from a const member function).
And certain member function needs reference (or better a const
reference).

for eg,
const PointRange* points = cc.points(ptAligned);

cc.points is a const member function which returns a const pointer to
class PointRange.
This member function (from a different class) needs a const reference
to PointRange.
convolveNearEdge(const PointRange& points,const Kernel& kernel, ...)
{}
I am passing it the reference as,
convolveNearEdge(*points,getKernel1(),velocity);

I do not need a null in the function thus a reference. also I make
assure points passed to convolveNearEdge is not null.

My question is, in the process do anywhere copy of the class occures
silently?

Unless there is a conversion from PointRange to something inside the
'convolveNearEdge' function... No, returning a pointer does not need
to make a copy, passing a reference doesn't either.
I have no problem if it copies a pointer. But do not want a
copy of the class even temporarily.

You can always put a breakpoint in the copy constructor and see if it
gets hit, you know...
Thus fundamentally , if I have a pointer, say int* x; *x = 10; then

Ahem... That's a bad idea. 'x' points to nothing and you try to
dereference it...
Very bad .... . May be this is the only reason I never use a pointer
which is not a class member. read the code as . int* x = new int; *x =
10; int& y = *x;
and at last delete x;
int& y = *x is essentially a reference to the value pointed by x? and
without any overload?

Yes, supposedly. Using 'y' after that is just like using '*x'.

V
Thus i feel that a pointer & reference can be freely interchangable
without any overhead provided the pointer is pointing to a valid
object.
I usually pass a const / non const reference as it always hold a valid
object & easier syntax. However I prefer return as pointer over
reference (const / non const) , when I am not supposed to return value,
because sometimes I forget to work with the reference, and use a copy
instead, which do not serve the purpose of returning reference.
like instead of writing
test11& t1 = t2.t();
occationally end up with
test11 t1 = t2.t();
:(
Any suggestion regurding this problem?

Thanks
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #4
toton wrote:
Victor Bazarov wrote:
>toton wrote:
>>[...]
Thus fundamentally , if I have a pointer, say int* x; *x = 10; then

Ahem... That's a bad idea. 'x' points to nothing and you try to
dereference it...
Very bad .... . May be this is the only reason I never use a pointer
which is not a class member. read the code as . int* x = new int; *x =
10; int& y = *x;
and at last delete x;
:-)

Usually

int* x = new int(10);

is preferred over

int* x = new int;
*x = 10;

It would be a good habit to get into...
>>int& y = *x is essentially a reference to the value pointed by x?
and without any overload?

Yes, supposedly. Using 'y' after that is just like using '*x'.

V
Thus i feel that a pointer & reference can be freely interchangable
without any overhead provided the pointer is pointing to a valid
object.
Well, yes, that's true. Remember, though, that if you want to use any
overloaded operators, you have to write (*ptr). Kinda ugly. But you
are right, in that case there is only the syntactical difference.
I usually pass a const / non const reference as it always hold a valid
object & easier syntax. However I prefer return as pointer over
reference (const / non const) , when I am not supposed to return
value, because sometimes I forget to work with the reference, and use
a copy instead, which do not serve the purpose of returning reference.
Returning a copy does serve its purpose, though. It's not the same as
returning a reference, and not only WRT copying.
like instead of writing
test11& t1 = t2.t();
occationally end up with
test11 t1 = t2.t();
:(
Any suggestion regurding this problem?
"Regurding"?

Anyway, just be careful. If you want to weed out all places where you
initialise an object (instead of a reference), disable the copy c-tor
and the compiler will barf. [of course it will barf on legal copying
as well, and you'll have to deal with that]. After removing all those
unwanted object copy-initialisations, re-enable the copy c-tor (if you
need it, that is).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #5

Victor Bazarov wrote:
toton wrote:
Victor Bazarov wrote:
toton wrote:
[...]
Thus fundamentally , if I have a pointer, say int* x; *x = 10; then

Ahem... That's a bad idea. 'x' points to nothing and you try to
dereference it...
Very bad .... . May be this is the only reason I never use a pointer
which is not a class member. read the code as . int* x = new int; *x =
10; int& y = *x;
and at last delete x;

:-)

Usually

int* x = new int(10);

is preferred over

int* x = new int;
*x = 10;

It would be a good habit to get into...
>int& y = *x is essentially a reference to the value pointed by x?
and without any overload?

Yes, supposedly. Using 'y' after that is just like using '*x'.

V
Thus i feel that a pointer & reference can be freely interchangable
without any overhead provided the pointer is pointing to a valid
object.

Well, yes, that's true. Remember, though, that if you want to use any
overloaded operators, you have to write (*ptr). Kinda ugly. But you
are right, in that case there is only the syntactical difference.
I usually pass a const / non const reference as it always hold a valid
object & easier syntax. However I prefer return as pointer over
reference (const / non const) , when I am not supposed to return
value, because sometimes I forget to work with the reference, and use
a copy instead, which do not serve the purpose of returning reference.

Returning a copy does serve its purpose, though. It's not the same as
returning a reference, and not only WRT copying.
Yes, returning reference is different than returning copy. I am
comparing returning reference and returning pointer. Not them with
returning copy.
like instead of writing
test11& t1 = t2.t();
occationally end up with
test11 t1 = t2.t();
:(
Any suggestion regurding this problem?

"Regurding"?
Thus, when I need to return the class member object instead to
1) someone modify it (may be rare case)
2) const reference / pointer , just for user to use it.
Now if I need to return a object as reference just for modification (
or const object just for usage, and the object is big, and ''assume"
return value optimization is not there, so return by value is an
overhead)
Then,
If I have class like
lass test11{
public:
test11(int x){ }
private:
//test11(const test11& t);
};
class test22{
private:
test11* _t;
public:
test11& t(){
return *_t;
}
};

Now,
test22 t2;
test11& t1 = t2.t();
This statement serves purpose, as I want user to "use test11 to use and
modify" not a copy of it. (may be, because I want it, or because it is
big enough not to be copied)
However by mistake if the user writes,
test22 t2;
test11 t1 = t2.t();
He unknowingly gets a copy and modifies it.
The same may be true if I return a const reference.
One method to prevent such mistake is to make the copy ctor private, or
not defined. However that may not be the solution as,
1) test11 may need copy ctor (say, test22 stores a vector<test11>
instead a single one)
2) I am not interested to prevent copy permanently, I just want to warn
about the mistake done "unknowingly"
Two solution so far I can think,
1) return a pointer / const pointer instead of reference/ const
reference.
Yes, as you suggested, if test11 has overloaded operator then direct
use of the class may be writing one more line to convert the pointer to
reference and then use it.
2) marke the copy ctor explicit.
Thus the code
test11 t1 = t2.t(); wont work. One need to write either
test11& t1 = t2.t(); =which is what I wanted usually.
or test11 t1(t2.t()); =which shows user clearly wants a copy.
I am seeking some advice "regarding" this kind of problem.
>
Anyway, just be careful. If you want to weed out all places where you
initialise an object (instead of a reference), disable the copy c-tor
and the compiler will barf. [of course it will barf on legal copying
as well, and you'll have to deal with that]. After removing all those
unwanted object copy-initialisations, re-enable the copy c-tor (if you
need it, that is).
I do it whenever possible. I can not do it when I need one - to -many
or many-to-one composition (association, aggregation) , as I usually
use stl containers for that purpose. However for any one-to-one
association / aggregation that is possible.

Many many thanks for the reply.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #6
toton wrote:
[..]
Thus, when I need to return the class member object instead to
1) someone modify it (may be rare case)
2) const reference / pointer , just for user to use it.
Now if I need to return a object as reference just for modification (
or const object just for usage, and the object is big, and ''assume"
return value optimization is not there, so return by value is an
overhead)
Then,
If I have class like
lass test11{
public:
test11(int x){ }
private:
//test11(const test11& t);
};
class test22{
private:
test11* _t;
public:
test11& t(){
return *_t;
}
};

Now,
test22 t2;
test11& t1 = t2.t();
This statement serves purpose, as I want user to "use test11 to use
and modify" not a copy of it. (may be, because I want it, or because
it is big enough not to be copied)
However by mistake if the user writes,
test22 t2;
test11 t1 = t2.t();
He unknowingly gets a copy and modifies it.
Why "unknowingly"? Why don't you trust your user to do the right thing?
The same may be true if I return a const reference.
Yes. So?
One method to prevent such mistake is to make the copy ctor private,
or not defined. However that may not be the solution as,
1) test11 may need copy ctor (say, test22 stores a vector<test11>
instead a single one)
2) I am not interested to prevent copy permanently, I just want to
warn about the mistake done "unknowingly"
Two solution so far I can think,
1) return a pointer / const pointer instead of reference/ const
reference.
Thinking back, you're trying to prevent the user from doing what maybe
the user wholeheartedly intends. There is no reason. You help the user
to avoid copying by returning a reference. What the user does with it
is not your business any more.

Same with pointers.
Yes, as you suggested, if test11 has overloaded operator then direct
use of the class may be writing one more line to convert the pointer
to reference and then use it.
2) marke the copy ctor explicit.
Thus the code
test11 t1 = t2.t(); wont work. One need to write either
test11& t1 = t2.t(); =which is what I wanted usually.
or test11 t1(t2.t()); =which shows user clearly wants a copy.
I am seeking some advice "regarding" this kind of problem.
I think you're seeing a problem where there isn't any.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 13 '06 #7

Victor Bazarov wrote:
toton wrote:
[..]
Thus, when I need to return the class member object instead to
1) someone modify it (may be rare case)
2) const reference / pointer , just for user to use it.
Now if I need to return a object as reference just for modification (
or const object just for usage, and the object is big, and ''assume"
return value optimization is not there, so return by value is an
overhead)
Then,
If I have class like
lass test11{
public:
test11(int x){ }
private:
//test11(const test11& t);
};
class test22{
private:
test11* _t;
public:
test11& t(){
return *_t;
}
};

Now,
test22 t2;
test11& t1 = t2.t();
This statement serves purpose, as I want user to "use test11 to use
and modify" not a copy of it. (may be, because I want it, or because
it is big enough not to be copied)
However by mistake if the user writes,
test22 t2;
test11 t1 = t2.t();
He unknowingly gets a copy and modifies it.

Why "unknowingly"? Why don't you trust your user to do the right thing?
The same may be true if I return a const reference.

Yes. So?
One method to prevent such mistake is to make the copy ctor private,
or not defined. However that may not be the solution as,
1) test11 may need copy ctor (say, test22 stores a vector<test11>
instead a single one)
2) I am not interested to prevent copy permanently, I just want to
warn about the mistake done "unknowingly"
Two solution so far I can think,
1) return a pointer / const pointer instead of reference/ const
reference.

Thinking back, you're trying to prevent the user from doing what maybe
the user wholeheartedly intends. There is no reason. You help the user
to avoid copying by returning a reference. What the user does with it
is not your business any more.

Same with pointers.
Yes, as you suggested, if test11 has overloaded operator then direct
use of the class may be writing one more line to convert the pointer
to reference and then use it.
2) marke the copy ctor explicit.
Thus the code
test11 t1 = t2.t(); wont work. One need to write either
test11& t1 = t2.t(); =which is what I wanted usually.
or test11 t1(t2.t()); =which shows user clearly wants a copy.
I am seeking some advice "regarding" this kind of problem.

I think you're seeing a problem where there isn't any.
May be. May not be also.
The reason is that, not every one is an expert C++ programmer. Even a
large number is not at all a C++ programmer. While parameter passing by
reference enhances the program(it takes reference, even when exact
reference is not passed)
like
object o; //o is the object itself
foo(o); //takes the object itself. where void foo(object o);
foo_ref(o); //takes the reference. where void foo(const object& o);
So, unknowingly (i.e not going through the reference manual of the API)
user promotes the idea of rererence!
Where for return by reference, user unknowingly may ignore the idea of
reference.
Pointer is surely not the same case, as it has a syntax difference in
the function declaration and function calling, otherwise the compiler
will object. (May be this mistake forced C# to use ref keyward in both
function declaration and calling).
Expert programmer do not have a problem. But average programmer may
fall into silent trap with just an '&' difference.
One of the problem relating copy ctor as I said, is on the line of
constructor with a single argument. And copy constructor is also a
single argument constructor. So the same warning may get applied to
copy constructor also.
check http://www.horstmann.com/cpp/pitfalls.html explicit array
section.
Also one can notice how many times people ask for a solution for copy
ctor & assignment operator. And for large class most people give copy
ctor a deep copy while assignment as shallow one or otherways (like
blitz array) .

Anyway, the whole thing is a matter of taste. I thought about that, as
I checked many programmer do this mistake, and while asked for the
reason the answer was, "I do it all the time!" , "I hadn't checked the
API documentation", "Oh! sorry for the mistake", or even "I don't know
all the details, it is your duty to correct it!".
Surely it is not valid for you as an user (as you are one of the main
trouble shooter in this newsgroup , others are surely Kai-Uwe Bux,
Frederick Gotham, Bart, Rolf Magnus, mlimber). I know that C++ relies
on programer to write a correct program (on contrary Java forces it.
Just for comparison check Java & C++ container model, and iterator
model) , but sometimes the later one is benificial.
Again, it is the question , to whom you are addressing! . "One size
does not fit all." True for program as well as programmer!
Thanks
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '06 #8
toton wrote:
[..] not every one is an expert C++ programmer. Even a
large number is not at all a C++ programmer. While parameter passing
by reference enhances the program(it takes reference, even when exact
reference is not passed)
like
object o; //o is the object itself
foo(o); //takes the object itself. where void foo(object o);
foo_ref(o); //takes the reference. where void foo(const object& o);
So, unknowingly (i.e not going through the reference manual of the
API) user promotes the idea of rererence!
Where for return by reference, user unknowingly may ignore the idea of
reference.
Yes, that's so. Just like if you ride a bus, you trust the driver to
do the right thing, but you don't have the freedom to stop any time you
want or to go anywhere you want. When you drive yourself, you do have
the freedom, but you have to learn driving, you have to pass the exam,
you have to take more risks than a passenger in a bus...

Freedom doesn't come cheap. "Unknowingly" in this case to me is the
same as "purposely ignoring". Shouldn't be your problem.
[..]
Anyway, the whole thing is a matter of taste.
I do not agree. It's not. You give them a sophisticated tool (in the
form of a library with functions returning *references*). They do not
want or do not care to use it to its fullest extend. Who's to blame?
I thought about that, as
I checked many programmer do this mistake, and while asked for the
reason the answer was, [..]
Here is the truth: if you strive to protect the users of your "C++"
library from making those mistakes, you will *inevitably* recreate Java
with all its troubles and problems and niceties and goodness. There is
no free cheese, except in a mouse trap.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9422
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How...
5
3027
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
18
3507
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
7
1714
by: key9 | last post by:
Hi ALL. Basic Question. sample: class MyClass { ....
14
7155
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your...
2
2812
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
1
1093
by: Wavelet | last post by:
I had one function to access string array. I don't know to write it to make if functionable. BTW,the string will be access (read only) in funtion. Now I use: print_occur_archive_files(char...
29
3619
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
41
3607
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.