473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static member functions access to class members and methods

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! =-----
Jul 22 '05 #1
11 4555
Roger Leigh wrote:
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)!


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.

Jul 22 '05 #2
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh wrote:
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.

M4

Jul 22 '05 #3
Martijn Lievaart wrote:
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh wrote:
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.

Jul 22 '05 #4
On Tue, 20 Jan 2004 19:14:13 +0100, Rolf Magnus wrote:
Martijn Lievaart wrote:
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh wrote:

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.


My bad, I was referring to the original statement, not your explanation. I
should have made that clearer.

M4

Jul 22 '05 #5
Roger Leigh <${******@invalid.whinlatter.uklinux.net.invalid > wrote in message news:<87************@wrynose.whinlatter.uklinux.ne t>...
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".
The static functions can address only the static data of a class;
non-static data are unavailable to these functions. If non-static data
could be addressed, to which object would they belong? Any attempt
to access a normal class member will generate a compile time
error. Similarly, static functions cannot call non-static functions of
the class. All this is caused by the fact that static functions have
no this pointer.
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! =-----

Jul 22 '05 #6

"c++novice" <be**************@yahoo.com> wrote in message
news:34**************************@posting.google.c om...
Roger Leigh <${******@invalid.whinlatter.uklinux.net.invalid > wrote in message news:<87************@wrynose.whinlatter.uklinux.ne t>...
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".


The static functions can address only the static data of a class;
non-static data are unavailable to these functions. If non-static data
could be addressed, to which object would they belong? Any attempt
to access a normal class member will generate a compile time
error. Similarly, static functions cannot call non-static functions of
the class. All this is caused by the fact that static functions have
no this pointer.


No they can access any data and methods of the class - they just don't have
a 'this'.

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.
Are you certain because it seems to me that you shouldn't even be able to
create a row since you have
declared convert_impl pure virtual.

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! =-----

Jul 22 '05 #7
c++novice wrote:
Roger Leigh <${******@invalid.whinlatter.uklinux.net.invalid > wrote in
message news:<87************@wrynose.whinlatter.uklinux.ne t>...
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".
The static functions can address only the static data of a class;
non-static data are unavailable to these functions. If non-static data
could be addressed, to which object would they belong?


Any object that is available to the function.
Any attempt
to access a normal class member will generate a compile time
error. Similarly, static functions cannot call non-static functions of
the class.
Just like a non-member-function can access non-static data of a class, a
static member function can. If you're still not convinced, explain
this:

#include <iostream>

class X
{
public:
static void foo(const X& x)
{
std::cout << x.text;
}

const char* text;
};

int main()
{
X x;
x.text = "Hello world!\n";
X::foo(x);
}

X::text is a non-static member of X, and still the static member
function does access it.
All this is caused by the fact that static functions have
no this pointer.


Right, they have no this pointer, but they can still access non-static
members of the class if they get an instance of that class. They just
are not called for a specific instance.

Jul 22 '05 #8
Rolf Magnus <ra******@t-online.de> wrote in message news:<bu*************@news.t-online.com>...
c++novice wrote:
Roger Leigh <${******@invalid.whinlatter.uklinux.net.invalid > wrote in
message news:<87************@wrynose.whinlatter.uklinux.ne t>...
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".
The static functions can address only the static data of a class;
non-static data are unavailable to these functions. If non-static data
could be addressed, to which object would they belong?


Any object that is available to the function.
Any attempt
to access a normal class member will generate a compile time
error. Similarly, static functions cannot call non-static functions of
the class.


Just like a non-member-function can access non-static data of a class, a
static member function can. If you're still not convinced, explain
this:

#include <iostream>

class X
{
public:
static void foo(const X& x)
{
std::cout << x.text;
}

const char* text;
};

int main()
{
X x;
x.text = "Hello world!\n";
X::foo(x);
}

X::text is a non-static member of X, and still the static member
function does access it.


U r very much right because here an object of type X is passed in
static member function foo()as a parameter,
But foo() is not directly accessing the member like
std::cout << text;

All this is caused by the fact that static functions have
no this pointer.


Right, they have no this pointer, but they can still access non-static
members of the class if they get an instance of that class. They just
are not called for a specific instance.

Jul 22 '05 #9
Rolf Magnus <ra******@t-online.de> writes:
Martijn Lievaart wrote:
On Tue, 20 Jan 2004 12:48:55 +0100, Rolf Magnus wrote:
Roger Leigh wrote:>

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.


Much of the book is like that. I found it fairly readable for the
basics of the language, but it leaves a lot of questions unanswered,
and the devil is always in the details.

I did get a copy of Stroustrup's TCPL from the library at the same
time, but as a C programmer with no OO knowledge, I was out of my
depth several pages into the introduction!! It would probably be OK
now I have a decent grasp of the language, but the 21 days books was
far more understandable at the time.
--
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! =-----
Jul 22 '05 #10
Rolf Magnus <ra******@t-online.de> writes:
Roger Leigh wrote:
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: [...] 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)!


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.


Ah, that makes sense!

I've now got it working, but only after discovering a wart in the
language: the base class is a template and if I declare a pure virtual
(or virtual) function in the template, when I call the function it
does not call the derived version. It appears that virtual does not
work with templates at all :-\.

I've come up with two solutions: declare the function in the deriving
class public, so the template can call it directly (not desirable), or
keep it protected/private and make the base class a friend, so it can
call them even though they are protected (better, but still not
clean enough for my liking).

Is there a better alternative to these approaches? Basically, I'd
like to be able to fake virtual to achieve this effect:

template<typename T>
class foo {
public:
void func() { func_impl(); }
private:
virtual void func_impl();
}

class bar : public foo<int> {
private:
virtual void func_impl();
}

i.e. when I do this:
bar c;
c.func()
c.func_impl() gets called by foo<int>::func().

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! =-----
Jul 22 '05 #11
Roger Leigh wrote:
I did get a copy of Stroustrup's TCPL from the library at the same
time, but as a C programmer with no OO knowledge, I was out of my
depth several pages into the introduction!! It would probably be OK
now I have a decent grasp of the language, but the 21 days books was
far more understandable at the time.


Try skipping the intro. If you already have some familiarity with C,
the first few chapters of TC++PL should be mostly review for you, and
get you up to speed quickly on C++ as a "better C."

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.