References as private members

I

Ivan Liu

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); }
};
 
V

Victor Bazarov

Ivan said:
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
 
F

Frederick Gotham

Ivan Liu posted:
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); }
};


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(); }
};
 
V

Victor Bazarov

Frederick said:
[..]
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
 
F

Frederick Gotham

Victor Bazarov posted:
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) ) {}
 
F

Frederick Gotham

Frederick Gotham posted:
MyClass(T *pA) : obj( *ThrowIfNull(pA) ) {}


Actually I would rather simply:

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

Frederick Gotham

Frederick Gotham posted:
Frederick Gotham posted:



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) ) {}
 
S

Sabiyur

Frederick said:
Ivan Liu posted:





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(); }
};

No need of Pointer in ctor...

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

Thanks
Sabiyur
 
I

Ivan Liu

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?
 
L

Lyell Haynes

Ivan said:
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.
 
M

mlimber

Ivan said:
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:

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
 
H

Howard

Ivan Liu said:
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:

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
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top