const reference for get accessor functions

  • Thread starter michaelkatsilis
  • Start date
M

michaelkatsilis

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
 
M

Mike Wahler

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
 
E

E. Robert Tisdale

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;
#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.
 
M

michaelkatsilis

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
 
E

E. Robert Tisdale

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.
 
M

michaelkatsilis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,541
Members
48,278
Latest member
OSWDelmar

Latest Threads

Top