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

const reference for get accessor functions

Hi,

Are there any issues with returning a const reference value for public
"get" accessor methods?

eg.

const string & getVal(); // or
const MyString & getVal();

Regards,

Michael

Jul 22 '05 #1
6 2656
Stupid question, I don't want to break encapsulation.

Jul 22 '05 #2

<mi*************@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi,

Are there any issues with returning a const reference value for public
"get" accessor methods?
Dpends upon what you mean by 'issues'. Since the reference
is to 'const', then you're protected against modification
of the referred to object.

eg.

const string & getVal(); // or
const MyString & getVal();


This is the typical way to return a 'read only' value
from a private part of a class. Another way would
be to return by value (but with more 'complex' types,
a reference will likely have better performance.)

-Mike
Jul 22 '05 #3
mi*************@yahoo.com wrote:
Are there any issues with returning a const reference value
for public "get" accessor methods?

eg.

const std::string& getVal(void) const; // or
const MyString& getVal(void) const; cat X.h #ifndef GUARD_X_H
#define GUARD_X_H 1

#include <string>

class X {
public:
class MyString: public std::string {
public:
// constructors
MyString(const std::string& s):
std::string(s) { }
};
private:
// representation
MyString S;
public:
// functions
const
MyString& getVal(void) const { return S; }
// constructors
X(const MyString& s): S(s) { }
};

#endif//GUARD_X_H 1
cat main.cc #include <X.h>
#include <iostream>

int main(int argc, char* argv[]) {

X x(std::string("Michael Katsilis"));
std::cout << x.getVal() << std::endl;

return 0;
}
g++ -I. -Wall -ansi -pedantic -o main main.cc
./main

Michael Katsilis

Assuming that X is *not* a container class
for objects of type std::string,
you (the class library developer)
*must* retain control over the definition of class MyString.
I have nested the definition of class MyString
inside the definition of class X to emphasize this point.
I have made std::string a *public* base class for MyString.
This means, in general, that MyString *must* provide
every method provided by std::string.
You probably won't want to do that.
You should probably make whatever representation
you want for MyString private and provide *only*
the methods that MyString should have.
That way, you should be able to substitute
any representation for MyString that you want
at a later date without breaking any code that uses class X.
Jul 22 '05 #4
Hi Robert,

You've explained what I was after. However, can you elaborate on the
class X wrapper please, I don't get it. Why wouldn't I have control
over the MyString class without it (see AString below)? For example,
Mike has implied that getVal below is read only. I've read elsewhere
that const MyString & GetVal() exposes the member (s in this case)...

#include <string>

class AString: public std::string
{
public:
AString(const std::string& s): std::string(s) { }
const AString& getVal(void) const { return s; }

private:
std::string s;
};

whereas returning the underlying data type by value would obviously not

#include <string>

class AString: public std::string
{
public:
AString(const std::string& s): std::string(s) { }
AString getVal(void) const { return s; }
private:
std::string s;
};

would not.

Regards,

Michael

Jul 22 '05 #5
mi*************@yahoo.com wrote:
You've explained what I was after.
However, can you elaborate on the class X wrapper please?
class X is *not* a wrapper.
I don't get it.
You are confused.
Why wouldn't I have control over the MyString class without it?
See AString below.
For example, Mike has implied that getVal below is read only.
You could define

std::string& getVal(void) { return s; }

as well but you might want to give it a different name.
I've read elsewhere that
const MyString & GetVal() exposes the member(s) in this case. cat AString.cc #include <string>

class AString: public std::string {
public:
AString(const std::string& s): std::string(s) { }
const
AString& getVal(void) const { return s; }
private:
std::string s;
};
g++ -Wall -ansi -pedantic -c AString.cc AString.cc: In member function \
`const AString& AString::getVal() const':
AString.cc:6: warning: returning reference to temporary

You probably meant
cat AString.cc #include <string>

class AString: public std::string {
public:
AString(const std::string& s): std::string(s) { }
const
std::string& getVal(void) const { return s; }
private:
std::string s;
};

You should *not* return a reference to the private std::string s
unless AString is a container class
because you have no control over the definition of a std::string.
You can't substitute a different data representation for s
because applications which use your AString class
will depend upon s being a std::string.

If you want to return a reference to a private data member,
define a new data type to represent it
and return a reference to an object of that type.
cat AString.cc #include <string>

class AString {
public:
class MyString: public std::string {
public:
// constructors
MyString(const std::string& s):
std::string(s) { }
};
private:
// representation
MyString S;
public:
AString(const std::string& s): S(s) { }
const
MyString& getVal(void) const { return S; }
};

Now you can change the representation for AString::MyString
whenever you like without any impact upon application which use it
except that they must be recompiled an relinked
with the latest release of your class library.
whereas returning the underlying data type by value would obviously not cat AString.cpp #include <string>

class AString: public std::string {
public:
AString(const std::string& s): std::string(s) { }
AString getVal(void) const { return s; }
private:
std::string s;
};
g++ -Wall -ansi -pedantic -c AString.cpp would not.


Please note that neither example that you gave
initializes std::string s so getVal returns
a reference to an empty string and
an empty string in the respective examples.
Jul 22 '05 #6
Hi Robert,

That's great, thanks for explaining it further I really appreciate it.
Please note that neither example that you gave
initializes std::string s so getVal returns
a reference to an empty string and
an empty string in the respective examples.


Yes, I should have filled it out further to make for a better thread in
this topic, but was only interested in understanding the getVal method.
I'll post better examples in future.

Regards,

Michael

Jul 22 '05 #7

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

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
7
by: Mark P | last post by:
The following compiles without error on four different platforms (Linux g++, Sun CC, HP aCC, Win Dev-C++) so I suspect it's ok, but I don't see why this isn't a const-related error. pB is a...
26
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...

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.