473,890 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private within this context, the mind of man is surely not this twisted ?

class a
{
public:
int x;
private:
int y;
}

class b : public a
{
public:
void b::somemethod()
}

void b::somemethod()
{
y = 20; // <- compiler error, what? no, the mind of man is surely
not this twisted
}

I was expecting b to have everything a has and "somemethod "
WTF ?

Oct 11 '06 #1
10 14865
Greg wrote:
class a
{
public:
int x;
private:
int y;
}
;
>
class b : public a
{
public:
void b::somemethod()
;
}
;
>
void b::somemethod()
{
y = 20; // <- compiler error, what? no, the mind of man is surely
not this twisted
Huh? 'y' is private. It cannot be accessed *except* from a member
of 'a' or from its *friend*. 'b' is not a member and not a friend.
That's why 'y' is not accessible.
}

I was expecting b to have everything a has and "somemethod "
WTF ?
Read your favorite C++ book again, especially the part where
'private' is explained.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 11 '06 #2

Greg wrote:
class a
{
public:
int x;
private:
int y;
}

class b : public a
{
public:
void b::somemethod()
}

void b::somemethod()
{
y = 20; // <- compiler error, what? no, the mind of man is surely
not this twisted
}

I was expecting b to have everything a has and "somemethod "
WTF ?

A derived class cannot access private members in the base class. The
derived class *can* access public and protected members in the base.
The only way for a derived class to access private members in the base
is for the base class to declare the derived class (or at least the
member function in question) a friend.

Oct 11 '06 #3
Bleargh, my C++ hateometer is at 8.6, that design is cac. So how do I
derive a class that can access the private members ? Or how can I make
the same overall design and avoid this dilema ?

Oct 11 '06 #4

Victor Bazarov wrote:
Huh? 'y' is private. It cannot be accessed *except* from a member
of 'a' or from its *friend*. 'b' is not a member and not a friend.
That's why 'y' is not accessible.

Read your favorite C++ book again, especially the part where
'private' is explained.
Yes, I misunderstand "private", I thought it meant private to the users
(scope) of the object but it obviously means more than that. Can you
declare b as a friend of a like this ?
class b : friend a
{
// unbleargh, maybe ?
};

Oct 11 '06 #5
Greg wrote:
Bleargh, my C++ hateometer is at 8.6, that design is cac.
You truly don't know C++ very well if you still score < 9.0.
So how do I
derive a class that can access the private members ?
Don't. That's what encapsulation means: You can change private members of
one class without wondering to yourself what other classes you just screwed
up - even if they are derived classes.

If you simply must do what you are doing, use 'protected'.

However, in general, one class should use the "tell, don't ask" when using
another class - eeeven a base class. The client class should not ask the
servant class what its 'y' value is. It should tell the servant class what
high-level operation it should do. That way, the servant class can use 'y'
the way it likes, and 'y' has a known and narrow scope. Eeeeeeeven if the
servant class is also a base class.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 11 '06 #6
Greg wrote:
Bleargh, my C++ hateometer is at 8.6, that design is cac. So how do I
derive a class that can access the private members ? Or how can I make
the same overall design and avoid this dilema ?
Make those members of the base class that should be visible to derived
classes protected instead of private. They will still be inaccessible from
classes outside the hierarchy.
Best

Kai-Uwe Bux
Oct 11 '06 #7
"Greg" <im*****@hotmai l.co.ukwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
Victor Bazarov wrote:
Huh? 'y' is private. It cannot be accessed *except* from a member
of 'a' or from its *friend*. 'b' is not a member and not a friend.
That's why 'y' is not accessible.

Read your favorite C++ book again, especially the part where
'private' is explained.

Yes, I misunderstand "private", I thought it meant private to the users
(scope) of the object but it obviously means more than that. Can you
declare b as a friend of a like this ?
class b : friend a
{
No. There's not much point in making something private if there's an easy way to get around it. Only
a can choose who its friends are. Private means _private_.
// unbleargh, maybe ?
};
DW
Oct 11 '06 #8
Kai-Uwe Bux wrote:.
Make those members of the base class that should be visible to derived
classes protected instead of private. They will still be inaccessible from
classes outside the hierarchy.
Can a derived class use this?

public:
using I_formerly_was_ protected;

If so, privacy is even less of an illusion of encapsulation.. .

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 11 '06 #9

Greg wrote:
Bleargh, my C++ hateometer is at 8.6, that design is cac. So how do I
derive a class that can access the private members ? Or how can I make
the same overall design and avoid this dilema ?
If it's private, then its up to the base class to give you access to
it. Why is it that you beleive that you can't access it?

class a
{
int y;
public:
void setY(int n) { y = n; }
int getY() const { return y; }
}; <- semicolon! its a class declaration

class b : public a
{
public:
void b::somemethod() const;
};

void b::somemethod()
{
setY(20);
}

Now consider a situation where you need to only allow y to be between 0
and 20, as an example. Why should the programmer be forced to have
those classes NOT responsible for the variable check that?
The point is that if y is protected, then class b must also impose the
0 <= y <= 20 verification. Thats would be a dumb strategy. The fact
that y above is private is helping the creator of the class determine
the rules of the design. Can you see that even an instance of class b
can now set/get y?

int main()
{
b bill;
bill.setY(11);
}

Oct 11 '06 #10

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

Similar topics

2
2770
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming methods of the two libraries: app.MainLoop, and reactor.run. Additionally, the fact that wxPython was to receive requests from the twisted framework as well as the end user seemed to be simply asking for trouble. My initial solution was, naturally,...
3
2234
by: Leszek Tumm | last post by:
Hi. Can you help solve a problem with operator overloading ? in one file i have definition of my class and declarations of overloaded operators: class Complex { long double Re;
0
568
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if link doesn't work, basically here is the article: An Exploration Into Launching Context-Sensitive HTML Help with Topic IDs in VB.NET I spent this evening investigating the HTML Help API as implemented in
5
3694
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause a context switch? What is the kernel object used for blocking EndRead calls? Event and mutex cause a context switch even when the object is signaled and no wait is needed, usage of critical section prevent this switch from happening... what is the...
8
10107
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for a text box has 6 items, undo, cut, copy, paste, delete and select all. I would like to add one additional paste menu that opens a new sub menu with several optional text items that could be pasted. The items would be populated by my program but...
2
1366
by: Jimbo | last post by:
Hey Have a real problem here. I have a page that consists of a lot of div tags and everything on the page is made in controls. In one of the controls we call a method in another class that produces some XML, In the end of that method we call transform to render the xml ive built up in .net into HTML from the xsl stylesheet. The output is
2
8015
by: ma740988 | last post by:
I might be a little off topic but the experience level here is enough to give me solid guidance. So here's the code: #ifndef FOO_TEST_H #define FOO_TEST_H #include <string> #include <deque> #include <iostream>
3
3408
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and demonstrates(?) that the copy constructor is never called (at least no sign of its chatter on std::cout is visible). So - is it necessary to have a public copy constructor in order to use "function style" initializers for an array of objects? ...
1
1173
by: DaTurk | last post by:
Hi, if I declare a delegate as private, within a namespace, what's the scope?
0
9979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.