473,406 Members | 2,867 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,406 software developers and data experts.

need help on coding grammar about reference / pointer /instance.

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++

Suppose we have

class FooClass{

public:

/*
foo() // this is what I want to ask below
*/

private:
string str;
}
What I need "template" is:

1. Use a outside string (string*,string&) to replace string content in class
string* outp;
string outi;
string& outr;
how to write these three foo()?
2. 1 outside will get string content inside of class, but outside can not
change it, (return a copy/return const pointer)
2. 2 outside will get string content inside of class, but outside can change
it, (return a point/reference)

3. outside can get string which have processed in foo()
string* foo(){
string* temp;
temp = processof(str);
return temp*
}
how to use autoptr to void mem leak? if I forgot delete temp* outside?
your help will save my life:-)

key9

keep trying ......
May 27 '06 #1
14 2382
On 2006-05-27 10:06, key9 wrote:
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++
You should be careful when talking about templates in here since there
is something called templates in C++, but it's not what you want.
Suppose we have

class FooClass{

public:

/*
foo() // this is what I want to ask below
*/

private:
string str;
}
What I need "template" is:

1. Use a outside string (string*,string&) to replace string content in class
string* outp;
string outi;
string& outr;
how to write these three foo()?
If I understand you correctly what you are asking for is something lika
this:

// Using a pointer
FooClass::foo(string* s)
{
srt = *s;
}

// Using a reference
FooClass::foo(string& s)
{
str = s;
}

// Using a copy
FooClass::foo(string s)
{
str = s;
}

Whenever possible it is preferable to use a reference instead of a
pointer, and often instead of using a copy too.
2. 1 outside will get string content inside of class, but outside can not
change it, (return a copy/return const pointer)
2. 2 outside will get string content inside of class, but outside can change
it, (return a point/reference)

3. outside can get string which have processed in foo()
string* foo(){
string* temp;
temp = processof(str);
return temp*
}
how to use autoptr to void mem leak? if I forgot delete temp* outside?


Supposing str already has some value it can be returned like this:

// Using a pointer
string* FooClass::foo2()
{
return &str;
}

// Using a reference
string& FooClass::foo2()
{
return str;
}

// Using a copy
string FooClass::foo2()
{
return str;
}

When returning a pointer or reference the user can later change the vlue
of str without calling foo() since it can manipulate the pointer/
reference directly, thus it's often a good idea to return a copy (unless
you want this behaviour).

Notice also that you should never return a pointer or reference to a
object declared inside the function returning it since the object will
no exist after the function has returned but the pointer/reference will
still point to it.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 27 '06 #2
Erik Wikström <Er***********@telia.com> wrote:
If I understand you correctly what you are asking for is something lika
this:

// Using a pointer
FooClass::foo(string* s)
{
srt = *s;
}

// Using a reference
FooClass::foo(string& s)
{
str = s;
}

// Using a copy
FooClass::foo(string s)
{
str = s;
} Whenever possible it is preferable to use a reference instead of a
pointer, and often instead of using a copy too.


Okay, this is a point of C++ style I don't understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people's code. Is a copy argument
less efficient? (Seems unlikely to me.)

thanks,

Steve
May 27 '06 #3
Steve Pope wrote:
Okay, this is a point of C++ style I don't understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people's code. Is a copy argument
less efficient? (Seems unlikely to me.)


The rule is: don't copy when you don't have to. Most of the times,
you'll want to pass objects by const reference.
Jonathan

May 27 '06 #4
On 2006-05-27 19:19, Steve Pope wrote:
Erik Wikström <Er***********@telia.com> wrote:
If I understand you correctly what you are asking for is something lika
this:

// Using a pointer
FooClass::foo(string* s)
{
srt = *s;
}

// Using a reference
FooClass::foo(string& s)
{
str = s;
}

// Using a copy
FooClass::foo(string s)
{
str = s;
}

Whenever possible it is preferable to use a reference instead of a
pointer, and often instead of using a copy too.


Okay, this is a point of C++ style I don't understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people's code. Is a copy argument
less efficient? (Seems unlikely to me.)


A copy can be very inefficient if the object passed is large, in that
case it's better to pass a const reference, since you then don't have to
copy the object. Imagine for example passing a collection (vector, list
etc.) as a copy, with many objects in the collection the operation will
be very slow compared with a reference.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 27 '06 #5
Erik Wikström <Er***********@telia.com> wrote:
Steve Pope wrote:
Okay, this is a point of C++ style I don't understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people's code. Is a copy argument
less efficient? (Seems unlikely to me.)

A copy can be very inefficient if the object passed is large,
in that case it's better to pass a const reference, since you
then don't have to copy the object. Imagine for example passing
a collection (vector, list etc.) as a copy, with many objects
in the collection the operation will be very slow compared with
a reference.


But surely for arguments that are not scalars, only a pointer
gets passed even if it is not a reference argument. I cannot
picture the compiler pushing a copy of a huge object onto the
stack. This is why I say it is unlikely to be less efficient.

Steve
May 27 '06 #6
On 2006-05-27 20:32, Steve Pope wrote:
Erik Wikström <Er***********@telia.com> wrote:
Steve Pope wrote:

Okay, this is a point of C++ style I don't understand. (One of
many, I expect.) Why would one use a reference argument to a
function, unless one wanted the function to modify the referent?
I frequently see this in people's code. Is a copy argument
less efficient? (Seems unlikely to me.)

A copy can be very inefficient if the object passed is large,
in that case it's better to pass a const reference, since you
then don't have to copy the object. Imagine for example passing
a collection (vector, list etc.) as a copy, with many objects
in the collection the operation will be very slow compared with
a reference.


But surely for arguments that are not scalars, only a pointer
gets passed even if it is not a reference argument. I cannot
picture the compiler pushing a copy of a huge object onto the
stack. This is why I say it is unlikely to be less efficient.


That's the power of C++, it lets you decide how to handle things, if you
pass a copy and make changes to it it is guaranteed that those changes
will not apply to the object you made a copy of. So even if the compiler
should optimize so that just a pointer/reference is passed it will still
have to make a copy.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 27 '06 #7
Erik Wikström <Er***********@telia.com> wrote:
On 2006-05-27 20:32, Steve Pope wrote:
But surely for arguments that are not scalars, only a pointer
gets passed even if it is not a reference argument. I cannot
picture the compiler pushing a copy of a huge object onto the
stack. This is why I say it is unlikely to be less efficient.

That's the power of C++, it lets you decide how to handle things, if you
pass a copy and make changes to it it is guaranteed that those changes
will not apply to the object you made a copy of. So even if the compiler
should optimize so that just a pointer/reference is passed it will still
have to make a copy.


Yes, I agree. I suppose the compiler could deal with this by
only creating the copy if the argument is either is used as a lhs, or
has its address taken within the function.

In any case I think you've explained the programming practice of
using reference arguments and then not modifying them. Thanks.

Steve
May 27 '06 #8
Steve Pope wrote:
Erik Wikström <Er***********@telia.com> wrote:
On 2006-05-27 20:32, Steve Pope wrote:

But surely for arguments that are not scalars, only a pointer
gets passed even if it is not a reference argument. I cannot
picture the compiler pushing a copy of a huge object onto the
stack. This is why I say it is unlikely to be less efficient.

That's the power of C++, it lets you decide how to handle things, if you
pass a copy and make changes to it it is guaranteed that those changes
will not apply to the object you made a copy of. So even if the compiler
should optimize so that just a pointer/reference is passed it will still
have to make a copy.


Yes, I agree. I suppose the compiler could deal with this by
only creating the copy if the argument is either is used as a lhs, or
has its address taken within the function.


No, it cannot do that. An optimizer could discard unused parameters in
some circumstances, but usually the compiler cannot (and should not)
assume the side effects of copying an object are trivial. When you pass
arguments by value, you must assume they will always be copied.
Jonathan

May 28 '06 #9
Jonathan Mcdougall wrote:
No, it cannot do that. An optimizer could discard unused parameters in
some circumstances, but usually the compiler cannot (and should not)
assume the side effects of copying an object are trivial. When you pass
arguments by value, you must assume they will always be copied.


I thought "return value optimization" applied there.

Even if it doesn't, side-effects in copy methods are Bad Things, anyway...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!l
May 28 '06 #10
Jonathan Mcdougall <jo***************@gmail.com> wrote:
Steve Pope wrote:
Yes, I agree. I suppose the compiler could deal with this by
only creating the copy if the argument is either is used as a lhs, or
has its address taken within the function.

No, it cannot do that.


Sure it can. By textual analysis, a compiler could rule out
some such instances.

Steve
May 28 '06 #11
Steve Pope wrote:
Jonathan Mcdougall <jo***************@gmail.com> wrote:
Steve Pope wrote:

Yes, I agree. I suppose the compiler could deal with this by
only creating the copy if the argument is either is used as a lhs, or
has its address taken within the function.

No, it cannot do that.


Sure it can. By textual analysis, a compiler could rule out
some such instances.


Would you care to provide an example where "textual analysis" could
allow a compiler to do something else than copying an argument passed
by value?
Jonathan

May 28 '06 #12
Phlip wrote:
Jonathan Mcdougall wrote:
No, it cannot do that. An optimizer could discard unused parameters in
some circumstances, but usually the compiler cannot (and should not)
assume the side effects of copying an object are trivial. When you pass
arguments by value, you must assume they will always be copied.


I thought "return value optimization" applied there.


Return value optimisation refers to the case when the return is by
value, whereas the return of an input argument can only logically be by
reference.

regards
Andy Little

May 28 '06 #13
Jonathan Mcdougall <jo***************@gmail.com> wrote:
Steve Pope wrote:
Jonathan Mcdougall <jo***************@gmail.com> wrote:
>Steve Pope wrote:

>> the compiler could deal with this by
>>only creating the copy if the argument is either is used as a
>>lhs, or has its address taken within the function. >No, it cannot do that.
Sure it can. By textual analysis, a compiler could rule out
some such instances.

Would you care to provide an example where "textual analysis" could
allow a compiler to do something else than copying an argument passed
by value?


Geez.
int func(std::vector<double> x, int a) {
std::printf("%f\n",x[0]);
return(2*a);
}

S.
May 28 '06 #14
Steve Pope wrote:
Jonathan Mcdougall <jo***************@gmail.com> wrote:
Steve Pope wrote:

Jonathan Mcdougall <jo***************@gmail.com> wrote:Steve Pope wrote:

>> the compiler could deal with this by
>>only creating the copy if the argument is either is used as a
>>lhs, or has its address taken within the function.No, it cannot do that. Sure it can. By textual analysis, a compiler could rule out
some such instances.

Would you care to provide an example where "textual analysis" could
allow a compiler to do something else than copying an argument passed
by value?


Geez.

int func(std::vector<double> x, int a) {
std::printf("%f\n",x[0]);
return(2*a);
}


I don't understand exactly. Can the vector be passed by something else
than by value here? If so, why? You may answer "because we know we're
not modifying x", but the compiler has no way to know that. A non-const
operator[] is called on the vector and it returns a non-const
reference.

What's more, replace std::vector<double> by T and all bets are off.
Jonathan

May 28 '06 #15

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

Similar topics

1
by: Karalius, Joseph | last post by:
Can anyone explain what is happening here? I haven't found any useful info on Google yet. Thanks in advance. mmagnet:/home/jkaralius/src/zopeplone/Python-2.3.5 # make gcc -pthread -c...
55
by: Alex | last post by:
Hello people, The following is not a troll but a serious request. I found myself in a position where I have to present a Pro/Con list to management and architects in our company with regard to...
11
by: Karl Irvin | last post by:
My program looks like this: Dim db As DAO.Database, rst As DAO.Recordset Set db = CurrentDb Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType = 'Invoice'") Do some...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
2
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about...
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.