Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

References as private members

4 views
Skip to first unread message

Ivan Liu

unread,
Jul 27, 2006, 9:33:33 AM7/27/06
to
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); }
};

Victor Bazarov

unread,
Jul 27, 2006, 9:43:42 AM7/27/06
to
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


Frederick Gotham

unread,
Jul 27, 2006, 9:53:12 AM7/27/06
to
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

Victor Bazarov

unread,
Jul 27, 2006, 9:57:13 AM7/27/06
to
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

Frederick Gotham

unread,
Jul 27, 2006, 10:26:18 AM7/27/06
to
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

Frederick Gotham

unread,
Jul 27, 2006, 10:28:57 AM7/27/06
to
Frederick Gotham posted:

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


Actually I would rather simply:

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


--

Frederick Gotham

Frederick Gotham

unread,
Jul 27, 2006, 10:30:32 AM7/27/06
to
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

Sabiyur

unread,
Jul 27, 2006, 10:59:25 AM7/27/06
to

No need of Pointer in ctor...

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

Thanks
Sabiyur

Sabiyur

unread,
Jul 27, 2006, 11:01:45 AM7/27/06
to
> Just change the Ctor as like this
> MyClass( T & A): Obj( a){
> }

Oh It is MyClass( T & A): obj( A){ }

Ivan Liu

unread,
Jul 27, 2006, 2:44:40 PM7/27/06
to
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?

Lyell Haynes

unread,
Jul 27, 2006, 3:01:41 PM7/27/06
to

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.

mlimber

unread,
Jul 27, 2006, 3:12:57 PM7/27/06
to
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

Howard

unread,
Jul 28, 2006, 12:03:49 PM7/28/06
to

"Ivan Liu" <dark...@gmail.com> wrote in message
news:1154025880....@m79g2000cwm.googlegroups.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


0 new messages