引用为私人成员 [英] References as private members
问题描述
我想知道如何在类的私有部分声明引用
并在成员函数中使用它。我知道我可以用指针来做但是
然后我必须使用指针语法,我希望避免使用。
例如,我会写什么如果我使用
引用而不是指针?
模板< class T>
类MyClass
{
私人:
T * ptr;
公开:
MyClass (T * ObjectA){ptr = ObjectA; }
double Get(){return ptr-> Function_0f_A(x); }
};
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); }
};
推荐答案
Ivan Liu写道:
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。
OK.
例如,如果我使用
引用而不是指针?
模板< class T>
类MyClass
{
私人:
T * ptr;
public:
MyClass(T * ObjectA){ptr = ObjectA ; }
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; }
你不能_assign_,你需要_initialise_参考。
此外,总是喜欢初始化过度分配。
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); }
};
double Get() { return ptr->Function_0f_A(x); }
};
V
-
请在通过电子邮件回复时删除资金'A'
我没有回复最热门的回复,请不要问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Ivan Liu发布:
Ivan Liu posted:
例如,如果我使用
引用而不是指针,我会为以下内容写什么?
For instance, what would I write for the following if I were to use
reference instead of a pointer?
< snip>
<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 * ptr;
public:
MyClass( T * ObjectA ){ ptr = ObjectA; }
double Get() { return ptr->Function_0f_A(x); }
};
模板< class T>
class MyClass {
private:
T& obj;
public:
MyClass(T * pA):obj(* pA){}
/ *或者也许:
MyClass(T& objA):obj(objA){}
* /
double Get(){return obj.Func(); }
};
-
Frederick Gotham
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
>
Frederick Gotham写道:
Frederick Gotham wrote:
[..]
模板< class T>
class MyClass {
私人:
T& obj;
公开:
MyClass(T * pA):obj(* pA){}
[..]
template<class T>
class MyClass {
private:
T &obj;
public:
MyClass(T *pA) : obj(*pA) {}
危险,Will Robinson!危险!!
您正在取消引用指针而不检查它是否为空。
要OP:这是指针的一个方面/参考转换你应该仔细考虑
!
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!
/ *或者也许:
MyClass (T& objA):obj(objA){}
* /
double Get(){return obj.Func() ; }
};
/* Or perhaps:
MyClass(T &objA) : obj(objA) {}
*/
double Get() { return obj.Func(); }
};
V
-
请在通过电子邮件回复时删除资金'A'
我没有回复最热门的回复,请不要问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于引用为私人成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!