static member函数访问类成员和方法 [英] static member functions access to class members and methods

查看:107
本文介绍了static member函数访问类成员和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须提供的C ++书籍(Liberty和Horvath,在21天内自学C ++

for Linux - 我知道还有更好的)声明静态

成员函数无法访问任何非静态成员变量"。

但是,这似乎并不完全正确。它也没有提到

静态成员函数是否可以访问受保护的和私有的
成员数据和方法(我在常见问题解答中无法看到这一点)。


我有一个课程行< Row>源自row_base:


模板< typename行>

类行:public row_base

{

public:

typedef std :: auto_ptr< Row> row_ptr;


row():row_base(){}

行(row_state status,bool modified = false):

row_base(状态,已修改){}

virtual~row(){}

static row_ptr create(pqxx :: result :: const_iterator row,

pqxxobject :: transaction& tran)

{

row_ptr p(new row);

p-> convert_impl(row,tran); //保护

p-> row_base :: m_state = STATE_INITIALISED; //私人

返回p;

}


virtual void convert_impl(pqxx :: result :: const_iterator row,

pqxxobject :: transaction& tran)= 0;


}; //类行


这里,在静态create()方法中,row_base :: m_state是基类的私有

(枚举)数据成员但是我可以直接分配它。

然而,convert_impl()是一个纯虚函数,这里是公共的

这里并在派生类中受到保护,但是我得到一个错误当我

编译:


places.cc:在静态成员函数中`static std :: auto_ptr< _Tp1>

pqxxobject :: row< Row> :: create(pqxx :: result :: const_i terator,

pqxxobject :: transaction&)[with Row = Place]'':

... / pqxx-object / table.h:172:从`std :: auto_ptr< std :: list< Row,std :: allocator< _CharT>实例化> > pqxxobject :: table< Row> :: find_many(const std :: string&)[with Row = Place]''

places.cc:207:从这里实例化

places.cc:175:错误:`virtual void

Place :: convert_impl(pqxx :: result :: const_iterator,pqxxobject :: transaction&)''

是保护

... / pqxx-object / row.h:97:错误:在此上下文中


row.h:97是p - > convert_impl(row,tran);"以上。


我无法看到能够访问私人数据的理由

成员,但不是受保护的方法(实际上是公开的)在

行<>类中)!

我可能会更好地使create()调用一个专门的

保护构造函数,但我我有兴趣了解为什么以上

不起作用。

谢谢,

罗杰


-

Roger Leigh


在GNU / Linux上打印? http://gimp-print.sourceforge.net/

GPG公钥:0x25BFB848。请签名并加密您的邮件。

----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

--- - ==超过100,000个新闻组 - 19个不同的服务器! = -----

The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn''t seem entirely correct. It also doesn''t mention
whether static member functions can access protected and private
member data and methods (and I couldn''t spot this in the FAQ).

I have a class row<Row> which derives from row_base:

template<typename Row>
class row : public row_base
{
public:
typedef std::auto_ptr<Row> row_ptr;

row(): row_base() {}

row(row_state status, bool modified=false):
row_base(status, modified) {}

virtual ~row() {}

static row_ptr create(pqxx::result::const_iterator row,
pqxxobject::transaction& tran)
{
row_ptr p(new Row);
p->convert_impl(row, tran); // protected
p->row_base::m_state = STATE_INITIALISED; // private
return p;
}

virtual void convert_impl(pqxx::result::const_iterator row,
pqxxobject::transaction& tran) = 0;

}; // class row

Here, in the static create() method, row_base::m_state is a private
(enum) data member of the base class, yet I can assign it directly.
However, convert_impl() is a pure virtual function which is public
here and protected in the deriving class, but I get an error when I
compile:

places.cc: In static member function `static std::auto_ptr<_Tp1>
pqxxobject::row<Row>::create(pqxx::result::const_i terator,
pqxxobject::transaction&) [with Row = Place]'':
.../pqxx-object/table.h:172: instantiated from `std::auto_ptr<std::list<Row, std::allocator<_CharT> > > pqxxobject::table<Row>::find_many(const std::string&) [with Row = Place]''
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_impl(pqxx::result::const_iterator, pqxxobject::transaction&)''
is protected
.../pqxx-object/row.h:97: error: within this context

row.h:97 is the "p->convert_impl(row, tran);" line, above.

I can''t see the rationale between being able to access private data
members, but not protected methods (which are actually public in the
row<> class)!
I would probably be better making create() call a specialised
protected constructor, but I''m interested in learning why the above
doesn''t work.
Thanks,
Roger

--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

推荐答案

Roger Leigh写道:
Roger Leigh wrote:
我要交的C ++书(Liberty和Horvath,在21天内为Linux自学C ++
- 我知道有更好的方法)声明静态
成员函数无法访问任何非静态成员变量。
但是,这似乎并不完全正确。


你是对的。如果你的静态成员函数有一个类的对象,

它可以访问该对象的成员变量。

它也没有提到静态成员函数是否可以访问
受保护的私人会员数据和方法(我在常见问题解答中无法发现这一点)。


会员是否静态无所谓。任何成员函数

都可以访问同一类的任何受保护和私有成员。

我有一个类行< Row>它来自row_base:

模板< typename行>
类行:public row_base
公共:
typedef std :: auto_ptr< Row> row_ptr;

row():row_base(){}
row(row_state status,bool modified = false):
row_base(status,modified){}

virtual~row(){}
静态row_ptr create(pqxx :: result :: const_iterator row,
pqxxobject :: transaction& tran)
{
row_ptr p(新行);
p-> convert_impl(row,tran); // protected
p-> row_base :: m_state = STATE_INITIALISED; //私有
返回p;


虚拟void convert_impl(pqxx :: result :: const_iterator row,
pqxxobject :: transaction& tran)= 0;

}; // class row

这里,在static create()方法中,row_base :: m_state是基类的私有
(枚举)数据成员,但我可以直接分配它。
但是,convert_impl()是一个纯粹的虚函数,在这里是公共的,并在派生类中受到保护,但是当我编译时出错:

places.cc:在静态成员函数`static std :: auto_ptr< _Tp1>
pqxxobject :: row< Row> :: create(pqxx :: result :: const_i terator,
pqxxobject :: transaction& )[with Row = Place]'':
../pqxx-object/table.h:172:从
`std :: auto_ptr< std :: list< row,std :: allocator<实例化; _CharT> > >
pqxxobject :: table< Row> :: find_many(const std :: string&)[with Row =
Place]''
places.cc:207:从这里实例化
places.cc:175:错误:`virtual void
Place :: convert_impl(pqxx :: result :: const_iterator,
pqxxobject :: transaction&)''受保护
。 ./pqxx-object/row.h:97:错误:在此上下文中

row.h:97是p-> convert_impl(row,tran);上面的行。

我看不出能够访问私人数据成员之间的理由,而不是受保护的方法(在
行中实际上是公开的< > class)!
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn''t seem entirely correct.
You''re right. If your static member function has an object of the class,
it can access member variables of that object.
It also doesn''t mention whether static member functions can access
protected and private member data and methods (and I couldn''t spot
this in the FAQ).
It doesn''t matter if the member is static or not. Any member function
can access any protected and private member of the same class.
I have a class row<Row> which derives from row_base:

template<typename Row>
class row : public row_base
{
public:
typedef std::auto_ptr<Row> row_ptr;

row(): row_base() {}

row(row_state status, bool modified=false):
row_base(status, modified) {}

virtual ~row() {}

static row_ptr create(pqxx::result::const_iterator row,
pqxxobject::transaction& tran)
{
row_ptr p(new Row);
p->convert_impl(row, tran); // protected
p->row_base::m_state = STATE_INITIALISED; // private
return p;
}

virtual void convert_impl(pqxx::result::const_iterator row,
pqxxobject::transaction& tran) = 0;

}; // class row

Here, in the static create() method, row_base::m_state is a private
(enum) data member of the base class, yet I can assign it directly.
However, convert_impl() is a pure virtual function which is public
here and protected in the deriving class, but I get an error when I
compile:

places.cc: In static member function `static std::auto_ptr<_Tp1>
pqxxobject::row<Row>::create(pqxx::result::const_i terator,
pqxxobject::transaction&) [with Row = Place]'':
../pqxx-object/table.h:172: instantiated from
`std::auto_ptr<std::list<Row, std::allocator<_CharT> > >
pqxxobject::table<Row>::find_many(const std::string&) [with Row =
Place]''
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_impl(pqxx::result::const_iterator,
pqxxobject::transaction&)'' is protected
../pqxx-object/row.h:97: error: within this context

row.h:97 is the "p->convert_impl(row, tran);" line, above.

I can''t see the rationale between being able to access private data
members, but not protected methods (which are actually public in the
row<> class)!




他们在行中是公开的<> class,但是你声明你的指针

为row_ptr,在你的情况下是auto_ptr< Place>。所以你是通过指向Place的指针来访问对象,正如你所说,

成员受到保护,即只放置自身和派生自

可以访问它。



They are public in the row<> class, but you are declaring your pointer
as row_ptr, which in your case is an auto_ptr<Place>. So you''re
accessing the object through a pointer to Place, where, as you say, the
member is protected, i.e. only Place itself and classes derived from
that can access it.


在星期二,2004年1月20日12:48:55 +0100,Rolf Magnus写道:< br>
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh写道:
Roger Leigh wrote:
我必须提供的C ++书籍(Liberty和Horvath,21岁时自学C ++
Days - 我知道有更好的说法声明静态
成员函数无法访问任何非静态成员变量。
然而,这似乎并不完全正确。
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn''t seem entirely correct.



你是对的。如果你的静态成员函数有一个类的对象,它可以访问该对象的成员变量。



You''re right. If your static member function has an object of the class,
it can access member variables of that object.




我想这是一个笨拙的方式说你没有这个指针。


M4



I guess it''s a clumsy way of saying you have no this-pointer.

M4


Martijn Lievaart写道:
Martijn Lievaart wrote:
2004年1月20日星期二12:48:55 +0100,Rolf Magnus写道:
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh写道:
Roger Leigh wrote:
我必须提供的C ++书籍(Liberty和Horvath,21天内为Linux自学C ++
- 我知道有更好的方法)声明静态
成员函数无法访问任何非静态成员变量"。
然而,这似乎并不完全正确。
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn''t seem entirely correct.



你是对的。如果你的静态成员函数有一个
类的对象,它可以访问该对象的成员变量。



You''re right. If your static member function has an object of the
class, it can access member variables of that object.



我想这是一种笨拙的方式,说你没有这个指针。



I guess it''s a clumsy way of saying you have no this-pointer.




这不是笨拙的,而是更加明确。无论如何,说静态

成员无法访问非静态成员是完全错误的。



It''s not clumsy, it''s just more explicit. Anyway, saying that static
members cannot access non-static members is just plain wrong.


这篇关于static member函数访问类成员和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆