473,545 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(referenc e /
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 2396
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(referenc e /
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(s tring* s)
{
srt = *s;
}

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

// Using a copy
FooClass::foo(s tring 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

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

Similar topics

1
5271
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 -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I. -I./Include -DPy_BUILD_CORE -o Modules/python.o Modules/python.c gcc -pthread -c...
55
3883
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 developing new products (specifically - desktop products) in C#/.NET instead of the usual C++/COM that we do. Since I am not an experienced .NET...
11
5978
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 work rst.close
4
3395
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any...
6
2798
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
1800
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 your characters into that save file. Anyhow, I want to extract a couple of lines of it and save it into a database and I need help on figuring...
8
9128
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& a):m_a(a){}
8
1846
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 following operator in my smartpointer class: .... operator ObjectType * () const
0
1792
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...
0
7457
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, 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...
0
7651
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, 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. ...
0
7802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7746
tracyyun
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...
0
5962
agi2029
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...
1
5320
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
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...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.