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

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 2024
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.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.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...@rocketmail.comwrote:
"Ook" <zoo...@gmail.comwrote in message

news:11**********************@h3g2000cwc.googlegro ups.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
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
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...
1
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:...
38
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...
67
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...
5
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...
4
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...
1
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...
0
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...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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,...

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.