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 4 1186
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
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
"xuatla" <xu****@gmail.c om> 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
Victor Bazarov wrote: "xuatla" <xu****@gmail.c om> 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 objects of my classes Person(superclass),
Student(inherit Person), Teacher(inherit Person) in that array.
The name will be the unique key. These classes are all working ok. I
want to be able to add, remove, find etc. objects.
To all of this, I...
|
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;
|
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 must be an object
instead of
|
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 receive a byte array as one of its
parameters. The project is marked for COM interop, and that all
proceeds normally.
When I reference the type library in the VB6 project, and write the
code to call the function that returns the byte array, it works
|
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 library built over top of another library which is
C API and my api is in C++. Thus any time I need to call any function
of this C-API, I need to pass the char * while I store them as string
as data member of my classes (see code below). To do so, I...
| |
by: al.cpwn |
last post by:
I get that these two are different
int* get()
{
static int m;
return &m;
}
int& get()
{
|
by: Kuku |
last post by:
What is the difference between a reference and a pointer?
|
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 think I need some "template".
Sorry for my coding experience in c++
|
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 I was understanding
the chapter on policy classes was to attempt to apply them to my project.
I understand the general concept of policies but I lack the knowledge and
wisdom of how to identify them in an existing project. So I figured to get
an...
|
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 overridden by the type. The
reason is because the CLR can just call these methods nonvirtually and System.ValueType
overrides all of these virtual methods and expects the value in the this argument to refer to an
unboxed value type instance. Remember, a...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |