473,394 Members | 1,869 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,394 software developers and data experts.

Access problem

Hi there

I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.

Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?

Any comments are appreciated.

Sam

class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};

class Derived : public Base {
public:
void doSomething( Base* base );
};

void Derived::doSomething( Base* base ) {
base.foo = 10;
}

int main() {
Base b;
Derived d;
d.doSomething( &b );
return 0;
}
Apr 5 '06 #1
3 1879
Samuel Burri wrote:
Hi there

I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.

Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?

Any comments are appreciated.

Sam

class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};

class Derived : public Base {
public:
void doSomething( Base* base );
};

void Derived::doSomething( Base* base ) {
base.foo = 10;
}


This should be base->foo = 10; not that it makes any difference, since
the code won't compile even if thus modified.

What you are doing here is access a member variable inside *another*
object (base) of the type Base; which is an access violation.

What you are trying to do (I guess) would be better expressed with

void Derived::doSomething( ) {
this->foo = 10;
}

<snip>

Thanks,
--GS

Apr 5 '06 #2
go****@hotmail.com schrieb:
Samuel Burri wrote:
Hi there

I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.

Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?

Any comments are appreciated.

Sam

class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};

class Derived : public Base {
public:
void doSomething( Base* base );
};

void Derived::doSomething( Base* base ) {
base.foo = 10;
}
This should be base->foo = 10; not that it makes any difference, since
the code won't compile even if thus modified.

You're right on that point of course.
What you are doing here is access a member variable inside *another*
object (base) of the type Base; which is an access violation.
Actually the object passed in, isn't of type base, but also of a derived
type.
What you are trying to do (I guess) would be better expressed with

void Derived::doSomething( ) {
this->foo = 10;
}

Well, that does not work, since I need access to both base classes (this
and the one passed by argument) for my function.

Any other hints? Or is there no easy solution I have to redo my whole
design?

Greets
Sam
Apr 5 '06 #3
Samuel Burri wrote:
go****@hotmail.com schrieb:
Samuel Burri wrote:
Hi there

I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.

Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?

Any comments are appreciated.

Sam

class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};

class Derived : public Base {
public:
void doSomething( Base* base );
};

void Derived::doSomething( Base* base ) {
base.foo = 10;
}

This should be base->foo = 10; not that it makes any difference, since
the code won't compile even if thus modified.

You're right on that point of course.
What you are doing here is access a member variable inside *another*
object (base) of the type Base; which is an access violation.

Actually the object passed in, isn't of type base, but also of a derived
type.
What you are trying to do (I guess) would be better expressed with

void Derived::doSomething( ) {
this->foo = 10;
}

Well, that does not work, since I need access to both base classes (this
and the one passed by argument) for my function.

Any other hints? Or is there no easy solution I have to redo my whole
design?


From C++/98 Standard

"11.5 Protected member access [class.protected]
1 When a friend or a member function of a derived class references a
protected nonstatic member of a base class, an access check applies in
addition to those described earlier in clause 11.102) Except when
forming a pointer to member (5.3.1), the access must be through a
pointer to, reference to, or object of the derived class itself (or any
class derived from that class) (5.2.5). If the access is to form a
pointer to member, the nestednamespecifier shall name the derived class
(or any class derived from that class)."

I'm not sure, but I think the text:

"Except when forming a pointer to member (5.3.1), the access must be
through a pointer to, reference to, or object of the derived class
itself (or any class derived from that class) (5.2.5)."

says, that the pointer you have must be a pointer to Derived (or any
derived from Derived), and not a pointer to Base.
Apr 5 '06 #4

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

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool including SourceOffSite, SourceAnyWhere and VSS Remoting This article makes a detailed...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
17
by: DaveG | last post by:
Hi all I am planning on writing a stock and accounts program for the family business, I understand this is likely to take close to 2 years to accomplish. The stock is likely to run into over a...
5
by: B1ackwater | last post by:
We've fooled around with Access a bit, but only using the single-user store-bought version. It seems to be a good database - versatile and infinitely programmable - and can apparently be used as a...
34
by: Mathieu Trentesaux | last post by:
Hello I downloaded Office 2007 for this reason : It seems, once again, that it is impossible to save any modification done in a VBA library, from the main project in Access. The save button...
21
by: Bigpond News | last post by:
Work at a large site - 1000+ PC's. Mixture of Win98 & WinXP. Majority of applications using Access 97. If I compile the Acc97 application on a Win98 PC, the .mde will run perfectly on both...
18
by: surfrat_ | last post by:
Hi, I am having the following problems in getting Microsoft Visual Studio 2005 Professional to link to an Access .mdb database. Please help me to sort this out. Problem 1: The Microsoft...
7
by: clintonG | last post by:
To all Microsoft partners and customers who have been unable to download recently or access ASP.NET documentation from the msdn2 website and for all of those customers who have been lied to and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.