473,386 Members | 1,705 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,386 software developers and data experts.

help: reference to a pointer

Hi,

I have a class

class myType
{
private:
int size;
double *elem;
.....
};

now I want to get access to "elem" directly outside the class.

I defined the following member functions:

(1) double* getele() { return elem; };

(2) double*& getele() { return elem; };

And I have a function as following:
void func( int n, double *v )
{
change v;
}

myType t;
func( n, t.getele() );

When I called func in above both ways of getele, "elem" are changed. I
think the reason is because "elem" is a pointer itself. Am I right?

But do I need to use reference (2) here? Is it better in any reasons?

Thanks for your instruction.

xuatla
Jul 22 '05 #1
4 1167
xuatla wrote:
I have a class

class myType
{
private:
int size;
double *elem;
....
};

now I want to get access to "elem" directly outside the class.
That's a rather bad idea.

I defined the following member functions:

(1) double* getele() { return elem; };

(2) double*& getele() { return elem; };
Why two of them? Or are they only two variations that you're testing?

And I have a function as following:
void func( int n, double *v )
{
change v;
Change 'v' in what way? 'v' is passed by value, changing the 'v' itself
is rather pointless here. Are you trying to change what 'v' points to?
}

myType t;
func( n, t.getele() );

When I called func in above both ways of getele, "elem" are changed. I
think the reason is because "elem" is a pointer itself. Am I right?
What? I have no idea what you meant by this paragraph.

But do I need to use reference (2) here? Is it better in any reasons?


Judging from the 'func' implementation, no, you don't need a reference.

Victor
Jul 22 '05 #2

sorry for my bad expression ...

I have wrote some in a reply to my another post.

Victor Bazarov wrote:
xuatla wrote:
I have a class

class myType
{
private:
int size;
double *elem;
....
};

now I want to get access to "elem" directly outside the class.

That's a rather bad idea.

I know...
I just thought it's easy. I set it as private instead of public to
protect it, but now I want to get access to it to simplify my work.
Maybe I should think about other ways.

I defined the following member functions:

(1) double* getele() { return elem; };

(2) double*& getele() { return elem; };

Why two of them? Or are they only two variations that you're testing?


Just for my test purpose. I want to know whether I have to use reference
here or not and which one is better.

What I want to do for elem is the change it after I call it from some
functions. And I think "keeping change" = "called by address or reference".

And I have a function as following:
void func( int n, double *v )
{
change v;

Change 'v' in what way? 'v' is passed by value, changing the 'v' itself
is rather pointless here. Are you trying to change what 'v' points to?
}
e.g.,
void func( int n, double *v )
{
for ( int i = 0; i < n; i++ ) v[i] = v[n-1-i];
}

v will be changed by func.
and I use func( n, t.getele() );

t.getele() is a double* and will be changed by func.

What I thought ( and is a mistake ) is that the two ways (1) double* getele() { return elem; }; t.getele() is just a "copy", passed by value, therefore after
func( n, t.getele() ), t.elem is not changed.
(2) double*& getele() { return elem; }; t.getele() is same as t.elem, therefore after func, t.elem is changed.

myType t;
func( n, t.getele() );

When I called func in above both ways of getele, "elem" are changed. I
think the reason is because "elem" is a pointer itself. Am I right?

What? I have no idea what you meant by this paragraph.

forget this. I made a mistake. restated in above. I made mistake on the
reference type in argument & return value.

But do I need to use reference (2) here? Is it better in any reasons?

Judging from the 'func' implementation, no, you don't need a reference.

Victor


Thanks a lot :) I am catching up now.

xuatla
Jul 22 '05 #3
"xuatla" <xu****@gmail.com> wrote...

sorry for my bad expression ...

I have wrote some in a reply to my another post.

Victor Bazarov wrote:
xuatla wrote:
I have a class

class myType
{
private:
int size;
double *elem;
....
};

now I want to get access to "elem" directly outside the class.

That's a rather bad idea.

I know...
I just thought it's easy. I set it as private instead of public to protect
it, but now I want to get access to it to simplify my work.
Maybe I should think about other ways.

I defined the following member functions:

(1) double* getele() { return elem; };

(2) double*& getele() { return elem; };

Why two of them? Or are they only two variations that you're testing?


Just for my test purpose. I want to know whether I have to use reference
here or not and which one is better.

What I want to do for elem is the change it after I call it from some
functions. And I think "keeping change" = "called by address or
reference".

And I have a function as following:
void func( int n, double *v )
{
change v;

Change 'v' in what way? 'v' is passed by value, changing the 'v' itself
is rather pointless here. Are you trying to change what 'v' points to?
}
e.g.,
void func( int n, double *v )
{
for ( int i = 0; i < n; i++ ) v[i] = v[n-1-i];
}

v will be changed by func.


No. 'v' will not be changed. The contents of the memory pointed to by 'v'
will. Do you understand te difference?
and I use func( n, t.getele() );

t.getele() is a double* and will be changed by func.
Again, no, the 'double*' will not be changed. The 'double', to which
that pointer points, will.

What I thought ( and is a mistake ) is that the two ways (1) double* getele() { return elem; }; t.getele() is just a "copy", passed by value, therefore after
func( n, t.getele() ), t.elem is not changed.
Correct.
(2) double*& getele() { return elem; };

t.getele() is same as t.elem, therefore after func, t.elem is changed.


If you only use that 'getele()' as an r-value, then it won't change. But
if you do

t.getele() = something;

then with a reference you will be able to change the value of the pointer
(but I don't think that's what you want, anyway).

Victor
Jul 22 '05 #4


Victor Bazarov wrote:
"xuatla" <xu****@gmail.com> wrote...
sorry for my bad expression ...

I have wrote some in a reply to my another post.

Victor Bazarov wrote:
xuatla wrote:
I have a class

class myType
{
private:
int size;
double *elem;
....
};

now I want to get access to "elem" directly outside the class.
That's a rather bad idea.
I know...
I just thought it's easy. I set it as private instead of public to protect
it, but now I want to get access to it to simplify my work.
Maybe I should think about other ways.

I defined the following member functions:

(1) double* getele() { return elem; };

(2) double*& getele() { return elem; };
Why two of them? Or are they only two variations that you're testing?


Just for my test purpose. I want to know whether I have to use reference
here or not and which one is better.

What I want to do for elem is the change it after I call it from some
functions. And I think "keeping change" = "called by address or
reference".

And I have a function as following:
void func( int n, double *v )
{
change v;
Change 'v' in what way? 'v' is passed by value, changing the 'v' itself
is rather pointless here. Are you trying to change what 'v' points to?
}


e.g.,
void func( int n, double *v )
{
for ( int i = 0; i < n; i++ ) v[i] = v[n-1-i];
}

v will be changed by func.

No. 'v' will not be changed. The contents of the memory pointed to by 'v'
will. Do you understand te difference?


O, I got it.
What I want is to change the "content". I misused the name of the
"pointer value" and "pointed value".
Thanks :-)

and I use func( n, t.getele() );

t.getele() is a double* and will be changed by func.

Again, no, the 'double*' will not be changed. The 'double', to which
that pointer points, will.

What I thought ( and is a mistake ) is that the two ways
(1) double* getele() { return elem; };


t.getele() is just a "copy", passed by value, therefore after
func( n, t.getele() ), t.elem is not changed.

Correct.

(2) double*& getele() { return elem; };


t.getele() is same as t.elem, therefore after func, t.elem is changed.

If you only use that 'getele()' as an r-value, then it won't change. But
if you do

t.getele() = something;

then with a reference you will be able to change the value of the pointer
(but I don't think that's what you want, anyway).

Now I see. I don't need to change the address of elem, just the value.

xuatla
Victor

Jul 22 '05 #5

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

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
6
by: mar00ned | last post by:
Hi, I have a written a custom allocator for STL, on the lines of default allocator as follows : template <class T> class pool_allocator { public: typedef size_t size_type;
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
5
by: Divick | last post by:
Hi all, I want to use std::string instead of char * 's as suggested at many places in this newsgroup as well as else where, but I am confused with the way to use it. For example I have a...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.