473,795 Members | 3,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Public to Private

Why would it be reasonable for someone to argue that it is incorrect to
allow a public member inherited from a public base class to be redefined as
private?
Nov 16 '06 #1
6 1840
Ajay Martin wrote:
Why would it be reasonable for someone to argue that it is incorrect
to allow a public member inherited from a public base class to be
redefined as private?
Could be due to the same views that cause people to continue the
discussions whether 'square' should inherit from 'rectangle' or
vice versa, and whether it should be done publicly or privately.

C++ is not an OO language. It's a language with elements of OOP.
Live and let live, I say. If somebody wants to argue against
redeclaring a public inherited member as private, let them prove
their point. But don't stop them from arguing.

Why would it be reasonable for a dog to lick its ...? Ask the
dog.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 16 '06 #2
Ajay Martin wrote:
Why would it be reasonable for someone to argue that it is incorrect to
allow a public member inherited from a public base class to be redefined as
private?
Firstly, it's "reasonable " in our industry for anyone to argue
anything. The ability to propose and debate techniques is a critical
aspect of being a software engineer.

Now discuss whether to actually do it.

There's no syntactic reason not to do that.

The technical reason is "principle of least surprise". The Liskov
Substitution Principle implies that two objects that share an interface
should share it semantically, not just syntactically. Client code,
calling those objects, shouldn't be able, in the normal course of the
client's activities, to tell the difference between the two items.

There are other ways to use object polymorphically besides pointers or
references to base classes. If you use one, such as generics, you might
surprise a client, when a public member suddenly becomes private.

So don't do that, and add it to the list of things your team knows not
to do.

--
Phlip

Nov 16 '06 #3

Ajay Martin wrote:
Why would it be reasonable for someone to argue that it is incorrect to
allow a public member inherited from a public base class to be redefined as
private?
Lets suppose that the derived class needs access to the public member
but protects the user of the class by not providing public access to
it. Why should that be forbidden? Why should a critical public member
be forced to expose itself? Its called encapsulation, is it not?
So reasonable it is not - its counter-productive. Its like saying that
deriving from base is a waist of time.

Now, if he/she argued that a virtual member function was made private,
then the arguement may hold. Although, even that could be warranted in
the case that the inheritance scheme is less than perfect.

Nov 16 '06 #4
Ajay Martin wrote:
Why would it be reasonable for someone to argue that it is incorrect to
allow a public member inherited from a public base class to be redefined as
private?
For me, it would be illogical and potentially confusing (I don't say
"incorrect" ) to do this, because the member may still be changed via a
pointer (or reference) to the base class type. So the derived class
will have to treat the member as though it were public anyway.

So to redefine the member as private won't add any meaningful access
control, and will be counter-intuitive for users of the derived class.
I can't think of a good reason to do it, but it's best to keep an open
mind in case a good reason turns up later.

Example:

struct Base
{
int member;
};

struct Derived : public Base
{
private:
using Base::member;
};

int main()
{
Derived derived;

// This is illegal: "member" is private in Derived
derived.member = 1;

// But access via pointer-to-base-class is OK!
Base *pb = &derived;
pb->member = 1;
}

Nov 16 '06 #5
* Pete C:
>
So to redefine the member as private won't add any meaningful access
control, and will be counter-intuitive for users of the derived class.
I can't think of a good reason to do it, but it's best to keep an open
mind in case a good reason turns up later.
In the case of a spaghetti system it's sometimes useful to replace

class Something: public Spaghetti
{
public:
...
};

with

class Something: protected Spaghetti
{
public:
Spaghetti& asSpaghetti() { return *this; }
Spaghetti const& asSpaghetti() const { return *this; }

using SpaghettiClass: :memberD;
using SpaghettiClass: :memberZ;
...
};

in order to ascertain what's actually /used/ (by Something's client
code) of all that stuff inherited from Spaghetti.

Of course one may end up finding that most of the spaghetti stuff is
used, in which case the gain is only knowing that class Something is not
a good place to start cleaning up the system, and any insights come by
while adding necessary 'using' clauses to get the thing to compile.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 16 '06 #6
Pete C wrote:
Ajay Martin wrote:
>Why would it be reasonable for someone to argue that it is incorrect
to allow a public member inherited from a public base class to be
redefined as private?

For me, it would be illogical and potentially confusing (I don't say
"incorrect" ) to do this, because the member may still be changed via a
pointer (or reference) to the base class type. So the derived class
will have to treat the member as though it were public anyway.

So to redefine the member as private won't add any meaningful access
control, and will be counter-intuitive for users of the derived class.
I can't think of a good reason to do it, but it's best to keep an open
mind in case a good reason turns up later.

Example:

struct Base
{
int member;
};

struct Derived : public Base
{
private:
using Base::member;
};

int main()
{
Derived derived;

// This is illegal: "member" is private in Derived
derived.member = 1;

// But access via pointer-to-base-class is OK!
Base *pb = &derived;
pb->member = 1;
}
You misread the question. It contains the term "redefined" . You just
re*declared* 'member' in 'Derived', you didn't *redefine* it. And for
what it's worth, everybody else seemed to think the question was about
member functions, not data.

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

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

Similar topics

3
2326
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
19
2351
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
6
3592
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
6
2565
by: Sahil Malik [MVP] | last post by:
Public Private Key Pairs - How do they work? ----------------------------------------------- I was looking at a presentation recently in which it was suggested that - User 1 Encrypts a message using User 2's Public Key. User 2 Decrypts the transmission using his Private Key to get the orignal message. Is the above correct?
10
3681
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global' variable for the application. Any other code within the application can access it. public - can be shared across the application if instatiated. Does that sound about right? It seems these are more useful for methods rather than variables. Most of...
4
1271
by: funVB2005fun | last post by:
I am not quite sure what I am getting into but I would like to have a loop that read from a flat file to create some public properties in a class. I am going after this to try and create more general software that others could benefit from vs hardcoding things that relate only to my processes. example property that I would want to generate: The idea would be to read from a file that "AGE" is needed and that it is and Integer and then to...
2
2441
by: Sky | last post by:
Hello: I'm trying to make sense of snk files, when to use, under what conditions to regenerate new ones,...can someone take a look if these statemes make sense? And then the final questions at the end that they first statements bring up in my mind... a) Because two developers, unbeknownst to each other, can end up releaseing different dll's with the same name, one should sign an assembly with a unique tag. right?
0
1048
by: Pilotflyhigh | last post by:
Hi, Please help. I have a class called Test public class Test { //This is my constructor public Test(string Code) { _code = Code; }
11
2443
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at the point is at the end of the program. //week 6 Inventory3 // by Tina Jones // IT 215 // october 07/2007
2
1786
by: fgh.vbn.rty | last post by:
Hi, I'm not sure if i'm asking the question correctly but anyway here it is. Say I have 3 classes - class A, class B, class R. 1) A and B are the building blocks and R is like a repository that stores objects of A and B. 2) A is at the lowest level and should "know about" only other As. B should know only about As and other Bs.
0
9519
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,...
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9037
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6779
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
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.