473,769 Members | 3,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const string *Item::getTitle () versus const string Item::getTitle( )

Ook
I have a function getStuff, and two choices of implementation:

const string *getStuff()
{
return &_stuff;
}

or

const string getStuff()
{
return _stuff;
}

where _stuffis just a string: string _stuff;

I can call the second one like this:

string zoot;
zoot = getStuff;

Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.

Mar 9 '07 #1
8 2048
Ook wrote:
I have a function getStuff, and two choices of implementation:

const string *getStuff()
{
return &_stuff;
}

or

const string getStuff()
{
return _stuff;
}

where _stuffis just a string: string _stuff;

I can call the second one like this:

string zoot;
zoot = getStuff;

Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.
You might want to use the first one if the string _stuff has a large
memory footprint. You call it by

string * zoot;
zoot = getStuff();
Mar 9 '07 #2
"Ook" <zo****@gmail.c omwrote in message
news:11******** **************@ h3g2000cwc.goog legroups.com...
>I have a function getStuff, and two choices of implementation:

const string *getStuff()
{
return &_stuff;
}

or

const string getStuff()
{
return _stuff;
}

where _stuffis just a string: string _stuff;

I can call the second one like this:

string zoot;
zoot = getStuff;

Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.
Normally, I return strings by value unless they are large. If I am to
return a pointer to a string, I would rather return a reference. So really
you have 3 choices then.

const string* getstuff1()
{
return &stuff_;
}

const string& getstuff2()
{
return stuff_;
}

string getstuff3()
{
return stuff_;
}

const string* zoot1 = getstuff1();
const string& zoot2 = getstuff2();
string zoot3 = getstuff3();

Weather it needs to be const or not depends on what you plan on doing with
it.

Incidently, do no use _ to prefix your variable names, there are many cases
where the names are reserved by the OS (_ and a capital, two __, etc..) I
find it much better to add the _ at the end of my class variables.
Mar 9 '07 #3
Ook
On Mar 9, 1:44 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Ook" <zoo...@gmail.c omwrote in message

news:11******** **************@ h3g2000cwc.goog legroups.com...
I have a function getStuff, and two choices of implementation:
const string *getStuff()
{
return &_stuff;
}
or
const string getStuff()
{
return _stuff;
}
where _stuffis just a string: string _stuff;
I can call the second one like this:
string zoot;
zoot = getStuff;
Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.

Normally, I return strings by value unless they are large. If I am to
return a pointer to a string, I would rather return a reference. So really
you have 3 choices then.

const string* getstuff1()
{
return &stuff_;

}

const string& getstuff2()
{
return stuff_;

}

string getstuff3()
{
return stuff_;

}

const string* zoot1 = getstuff1();
const string& zoot2 = getstuff2();
string zoot3 = getstuff3();

Weather it needs to be const or not depends on what you plan on doing with
it.

Incidently, do no use _ to prefix your variable names, there are many cases
where the names are reserved by the OS (_ and a capital, two __, etc..) I
find it much better to add the _ at the end of my class variables.
Hi, thanks both of your for the clairifcation. I use the _ to prefix
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?

Mar 9 '07 #4
>>
>>Incidently, do no use _ to prefix your variable names, there are many cases
where the names are reserved by the OS (_ and a capital, two __, etc..) I
find it much better to add the _ at the end of my class variables.


Hi, thanks both of your for the clairifcation. I use the _ to prefix
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?
Underscore to prefix class variables is perfectly legal C++. I use it
myself and I think it's a great convention. Jim is warning you that some
other uses of underscores in names are not legal. I never really
understood why the other illegal uses of underscore should count against
this legal use of underscore, but Jim isn't the only person to think
this way.

john

Mar 9 '07 #5
John Harrison wrote:
>>>
Incidently, do no use _ to prefix your variable names, there are many
cases
where the names are reserved by the OS (_ and a capital, two __,
etc..) I
find it much better to add the _ at the end of my class variables.


Hi, thanks both of your for the clairifcation. I use the _ to prefix
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?

Underscore to prefix class variables is perfectly legal C++. I use it
myself and I think it's a great convention. Jim is warning you that some
other uses of underscores in names are not legal. I never really
understood why the other illegal uses of underscore should count against
this legal use of underscore, but Jim isn't the only person to think
this way.
Because the next poor schmoe to maintain your code may refactor it out,
and leave the underscore in place in the global namespace.

Or you may get into the habit of a prefix on members, and have a member
such as _MyMember, which is not valid in any context.
Mar 10 '07 #6
red floyd wrote:
John Harrison wrote:
>>>>
Incidently , do no use _ to prefix your variable names, there are
many cases
where the names are reserved by the OS (_ and a capital, two __,
etc..) I
find it much better to add the _ at the end of my class variables.

Hi, thanks both of your for the clairifcation. I use the _ to prefix
private variables because that is how my first c++ instructor had us
do it <shrug>. Are there any generally accepted naming conventions for
variables in c++?

Underscore to prefix class variables is perfectly legal C++. I use it
myself and I think it's a great convention. Jim is warning you that
some other uses of underscores in names are not legal. I never really
understood why the other illegal uses of underscore should count
against this legal use of underscore, but Jim isn't the only person to
think this way.


Because the next poor schmoe to maintain your code may refactor it out,
and leave the underscore in place in the global namespace.
Class member variable into global variable is a pretty unusual
refactoring. I think I'd be worried about more than breaking the C++
naming rules.
>
Or you may get into the habit of a prefix on members, and have a member
such as _MyMember, which is not valid in any context.
The underscore is a prefix on members, I don't use it anywhere else.

john
Mar 10 '07 #7
Ook wrote:
>
I have a function getStuff, and two choices of implementation:

const string *getStuff()
{
return &_stuff;
}

or

const string getStuff()
{
return _stuff;
}

where _stuffis just a string: string _stuff;

I can call the second one like this:

string zoot;
zoot = getStuff;

Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.
It is easy to use

string getStuff(){ return _stuff; }

by the cost of possible perfomans lost. Read books about C++ and differences
between variables returned by value and variables returned by reference or
pointer.
--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 17 '07 #8
Grizlyk wrote:
Ook wrote:
>>
I have a function getStuff, and two choices of implementation:

const string *getStuff()
{
return &_stuff;
}

or

const string getStuff()
{
return _stuff;
}

where _stuffis just a string: string _stuff;

I can call the second one like this:

string zoot;
zoot = getStuff;

Why would I want to use the first one in the above examples, and how
would I call it? I can't just use zoot = getStuff because I get a
compiler error.

It is easy to use

string getStuff(){ return _stuff; }
And even probably

string getStuff() const { return _stuff; }
by the cost of possible perfomans lost. Read books about C++ and
differences between variables returned by value and variables
returned by reference or pointer.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 17 '07 #9

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

Similar topics

9
2600
by: deanfamily11 | last post by:
I have an ADT and I'd like to search for certain items in it. How can I accomplish this?
7
2957
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
1
1919
by: fakeprogress | last post by:
I am attempting to write a function (I shall call it findcode()) that makes sure that a code read in from a file is an actual code, one found within the library of books. Here is what I have: --- #include <iostream> #include <string> #include <vector> #include <fstream>
38
4239
by: JenniferT | last post by:
OK, so I'm very new to Java programming and I've been able to squeek by so far, but I'm completely stuck on this assignment. Here is the assignment that is due this week - • Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each...
67
23042
by: hollywoood | last post by:
I am trying to add the Delete button, save and search buttons. I have tried to call my teacher and he is absolutly no help and i have read and reread the text but still have no idea what is going on.........I have the added the add button but now i am stuck. Is there a simple way in completeing this?? Please help • Due Date: Day 7 forum • Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the...
5
2507
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is another way? /* * Main.java * * Created on April 29, 2007, 6:57 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
4
8877
by: im12345 | last post by:
I have the following question: Im doing a sample application using dojo and json. I have 2 classes: 1. Book class package com.esolaria.dojoex; import org.json.JSONObject; import org.json.JSONException;
1
2338
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After execution or error message, the program puts the cursor on the next line, and waits for the next command. i have two commands that I CANNOT SEEM TO GET TO WORK...addDVD and deleteDVD. I just learned that I can't use an array, because i "must"...
0
11616
by: JosAH | last post by:
A Simple Text-Based Menu System Read this this post; there are numerous posts like that: a newbie struggling with some sort of menu implementation. They want nested menus of course and an option to quit the entire thing. Of course they tie their 'business rules' tight together with their (feeble) attempts of their individual menu items and get heavily entangled in complicated control structures. The post (see the link above) made me write...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6673
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
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...
3
2815
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 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...

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.