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); }
};
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
> 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
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
>> 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
> MyClass(T *pA) : obj( *ThrowIfNull(pA) ) {}
Actually I would rather simply:
MyClass(T *pA) : obj( (assert(pA), *pA) ) {}
--
Frederick Gotham
> 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
No need of Pointer in ctor...
Just change the Ctor as like this
MyClass( T & A): Obj( a){
}
It works for me..
Thanks
Sabiyur
Oh It is MyClass( T & A): obj( A){ }
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?
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.
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
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