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

References as private members

Hi, I wonder how I can declare a reference in private part of a class
and use it in the member functions. I know I can do it by pointers but
then I have to use pointer syntax, which I hope to avoid.

For instance, what would I write for the following if I were to use
reference instead of a pointer?

template <class T>
class MyClass
{
private:
T * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
double Get() { return ptr->Function_0f_A(x); }
};

Jul 27 '06 #1
12 2248
Ivan Liu wrote:
Hi, I wonder how I can declare a reference in private part of a class
and use it in the member functions.
Sure.
I know I can do it by pointers but
then I have to use pointer syntax, which I hope to avoid.
OK.
For instance, what would I write for the following if I were to use
reference instead of a pointer?

template <class T>
class MyClass
{
private:
T * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
You won't be able to _assign_, you'll need to _initialise_ the reference.
Besides, always prefer initialisation over assignment.
double Get() { return ptr->Function_0f_A(x); }
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #2
Ivan Liu posted:
For instance, what would I write for the following if I were to use
reference instead of a pointer?
<snip>
template <class T>
class MyClass
{
private:
T * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
double Get() { return ptr->Function_0f_A(x); }
};

template<class T>
class MyClass {
private:
T &obj;

public:

MyClass(T *pA) : obj(*pA) {}

/* Or perhaps:

MyClass(T &objA) : obj(objA) {}

*/

double Get() { return obj.Func(); }
};

--

Frederick Gotham
Jul 27 '06 #3
Frederick Gotham wrote:
[..]
template<class T>
class MyClass {
private:
T &obj;

public:

MyClass(T *pA) : obj(*pA) {}
Danger, Will Robinson! Danger!!
You're dereferencing a pointer without checking if it's null or not.

To OP: this is one aspect of pointer/refernce conversion you should
carefully consider!
/* Or perhaps:

MyClass(T &objA) : obj(objA) {}

*/

double Get() { return obj.Func(); }
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '06 #4
Victor Bazarov posted:
> MyClass(T *pA) : obj(*pA) {}

Danger, Will Robinson! Danger!!
You're dereferencing a pointer without checking if it's null or not.

To OP: this is one aspect of pointer/refernce conversion you should
carefully consider!

Perhaps:

MyClass(T *pA) : obj(pA ? *pA : (throw -1,*pA) ) {}

Or:

class NullAsRef {};

template<class T>
T *ThrowIfNull(T *p)
{
if (!p) throw NullAsRef();

return p;
}
MyClass(T *pA) : obj( *ThrowIfNull(pA) ) {}

--

Frederick Gotham
Jul 27 '06 #5
Frederick Gotham posted:
MyClass(T *pA) : obj( *ThrowIfNull(pA) ) {}

Actually I would rather simply:

MyClass(T *pA) : obj( (assert(pA), *pA) ) {}
--

Frederick Gotham
Jul 27 '06 #6
Frederick Gotham posted:
Frederick Gotham posted:
> MyClass(T *pA) : obj( *ThrowIfNull(pA) ) {}


Actually I would rather simply:

MyClass(T *pA) : obj( (assert(pA), *pA) ) {}

Forgot to throw a const in there:

MyClass(T *const pA) : obj( (assert(pA),*pA) ) {}

--

Frederick Gotham
Jul 27 '06 #7

Frederick Gotham wrote:
Ivan Liu posted:
For instance, what would I write for the following if I were to use
reference instead of a pointer?

<snip>
template <class T>
class MyClass
{
private:
T * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
double Get() { return ptr->Function_0f_A(x); }
};


template<class T>
class MyClass {
private:
T &obj;

public:

MyClass(T *pA) : obj(*pA) {}

/* Or perhaps:

MyClass(T &objA) : obj(objA) {}

*/

double Get() { return obj.Func(); }
};

--

Frederick Gotham
No need of Pointer in ctor...

Just change the Ctor as like this
MyClass( T & A): Obj( a){
}
It works for me..

Thanks
Sabiyur

Jul 27 '06 #8

Sabiyur wrote:
Frederick Gotham wrote:
Ivan Liu posted:
For instance, what would I write for the following if I were to use
reference instead of a pointer?
<snip>
template <class T>
class MyClass
{
private:
T * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
double Get() { return ptr->Function_0f_A(x); }
};

template<class T>
class MyClass {
private:
T &obj;

public:

MyClass(T *pA) : obj(*pA) {}

/* Or perhaps:

MyClass(T &objA) : obj(objA) {}

*/

double Get() { return obj.Func(); }
};

--

Frederick Gotham

Just change the Ctor as like this
MyClass( T & A): Obj( a){
}
Oh It is MyClass( T & A): obj( A){ }

Jul 27 '06 #9
Thanks a lot.

However I don't quite understand the logic..
It seems like inehritance but I don't have much experience
and I don't understand the meaning of this simple line:

>
MyClass(T &objA) : obj(objA) {}
Frederick Gotham
why this line makes it ok to declare an undefined reference
in the private part of the class?

Jul 27 '06 #10

Ivan Liu wrote:
Hi, I wonder how I can declare a reference in private part of a class
and use it in the member functions. I know I can do it by pointers but
then I have to use pointer syntax, which I hope to avoid.
Keep in mind that with references, to be safe, the object that is being
referred to needs to have a lifetime greater than the object that is
storing the reference. With a pointer, you could transfer ownership of
the object, but with references, you can't.

Jul 27 '06 #11
Ivan Liu wrote:
Thanks a lot.

However I don't quite understand the logic..
It seems like inehritance but I don't have much experience
and I don't understand the meaning of this simple line:

MyClass(T &objA) : obj(objA) {}
See http://parashift.com/c++-faq-lite/ctors.html#faq-10.6
why this line makes it ok to declare an undefined reference
in the private part of the class?
References are very much like const pointers (e.g., const int * const
p1) in that they can't be "reseated" like non-const pointers (e.g.,
const int * p2) can, so they have to be initialized in the constructor
initializer list, along with any other non-static constants.

Cheers! --M

Jul 27 '06 #12

"Ivan Liu" <da*******@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Thanks a lot.

However I don't quite understand the logic..
It seems like inehritance but I don't have much experience
and I don't understand the meaning of this simple line:

>>
MyClass(T &objA) : obj(objA) {}
That line is a constructor for the MyClass class. The part ": obj(objA)" is
called the "initializer list".

The ":" specifies the start of the list, and is followed by a
comma-separated list of initializers. (In this case, there's only one
initializer: "obj(objA)".) The "obj" is the name of the member to be
initialized, and the "(objA)" part tells the compiler what to initialize obj
with. So this initializer list initializes the member obj to the reference
passed in as a parameter: objA.

This is the only way to initialize a member reference, since you can't
assign to a reference using "=". (Using "=" on a reference changes the
_value_ of what it refers to, it doesn't make it refer to something else,
like it does when using "=" on a pointer.)

Look up initializer lists in your favorite C++ textbook for more info and
examples.

-Howard
Jul 28 '06 #13

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

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
7
by: Dave Townsend | last post by:
Hi, I came across a little problem the other day, I wonder if anyone has a more elegant solution: I have a class which has some "pixmap" members, this is a QT-data type which is similar to a...
3
by: Simon Elliott | last post by:
A fairly standard polymorphic class which needs a reference to an object: class bar { int i1_; }; class fooBase {
10
by: jois.de.vivre | last post by:
Hi, I'm wondering exactly how the STL vector is making a copy of my object. For example: #include <iostream> #include <cstdlib> #include <vector> using namespace std; class test {
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
1
by: sip.address | last post by:
Hi everyone, A while ago I asked for help about using smart pointers (ie. shared_ptr) in cyclic situations. Weak ptrs popped out and I gave it a try, but to be honest, I don't feel very...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.