Apparently I'm missing something. Stroustrup (15.3) says of protected
access:
If [a member] is protected, its name can be used only by member functions
and friends of the class in which it is declared and by member functions and
friends of classes derived from this class.
Since private access cares about the calling class rather than the calling
object, I assumed the same was true for protected access, but the following
code fails in MSVC6:
class B
{
protected:
virtual void speak() {
std::cout << "Howdy, I'm B" << std::endl;
}
};
class D : public B
{
public:
void listen(B& other) {
std::cout << "It says: ";
other.speak();
}
};
// error C2248: 'speak' : cannot access protected member declared in class
'B'
Is this correct? So, protected access is granted only to the derived
object, rather than the derived class? Can I do something like this without
relying on public access? 2 10476
"Kevin Saff" <go********@kev in.saff.net> wrote... Apparently I'm missing something. Stroustrup (15.3) says of protected access:
If [a member] is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions
and friends of classes derived from this class.
Since private access cares about the calling class rather than the calling object, I assumed the same was true for protected access, but the
following code fails in MSVC6:
class B { protected: virtual void speak() { std::cout << "Howdy, I'm B" << std::endl; } };
class D : public B { public: void listen(B& other) { std::cout << "It says: "; other.speak(); } };
No, you can only access 'speak' in the same object as '*this'.
You cannot access 'speak' in another object because it may not
be your subobject. Imagine
class DD : public A, public B { ...whatever... };
D d;
DD dd;
d.listen(dd);
what would happen? You'd try to access a part of object from
a different hierarchy. // error C2248: 'speak' : cannot access protected member declared in class 'B'
Is this correct?
Yes.
So, protected access is granted only to the derived object, rather than the derived class?
Yes.
Can I do something like this without relying on public access?
Describe the problem you're trying to solve.
Victor
"Victor Bazarov" <v.********@att Abi.com> wrote in message
news:vi******** ****@corp.super news.com... "Kevin Saff" <go********@kev in.saff.net> wrote... Apparently I'm missing something. Stroustrup (15.3) says of protected access:
If [a member] is protected, its name can be used only by member
functions and friends of the class in which it is declared and by member functions and friends of classes derived from this class.
Since private access cares about the calling class rather than the
calling object, I assumed the same was true for protected access, but the following code fails in MSVC6:
class B { protected: virtual void speak() { std::cout << "Howdy, I'm B" << std::endl; } };
class D : public B { public: void listen(B& other) { std::cout << "It says: "; other.speak(); } };
No, you can only access 'speak' in the same object as '*this'. You cannot access 'speak' in another object because it may not be your subobject. Imagine
That is not true. Correct sentence would be:
"you can only access 'speak' in the object of the same class as '*this'".
The rule is exactly the same as for "private" - you can replace "protected"
by "private" in the example and try.
To make it compilable you have to change function 'listen' to accept
reference to the class "D" (or some other class derived from "D"):
void listen(D& other) {
std::cout << "It says: ";
other.speak();
}
[...] So, protected access is granted only to the derived object, rather than the derived class?
Yes.
No. It is granted to derived class. But you can access it only in the
objects
of the derived class - it in the objects of the base class.
Michael Furman This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. They don't
actually protect any encapsulation or anything, and for all the actual
protection they offer, they might as well be public.
For example:
class B {
protected:
|
by: yccheok |
last post by:
hello, in a singleton design, i trying to make my parent class having
protected constructor and destructor. however, the compiler give me
error when my child, trying to delete itself through parent pointer.
isn't child should have access to parent protected destructor?
thank you.
class CMachineFactory
|
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 experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of
ways to do this through prototyping and other techniques, but these
behaviors need to be static and...
|
by: Tina |
last post by:
This is an issue regarding what exactly is accomplished by using "Protected"
when defining a variable. It seems it does much more than just applying
Protected status to a variable.
I have an ascx control named HeadingBar. I have dragged it onto an aspx
page.
If I use the following statement.....
Dim HeadingBar1 as HeadingBar
|
by: mlimber |
last post by:
In an article on the safe bool idiom
(http://www.artima.com/cppsource/safeboolP.html), Bjorn Karlsson gives
the following code (slightly modified):
class safe_bool_base {
protected:
typedef void (safe_bool_base::*bool_type)() const;
void this_type_does_not_support_comparisons() const {}
safe_bool_base() {}
| |
by: Rick |
last post by:
Hi,
Can anyone explain to me why the below fails to compile - seeing
otherA->f(); as a call to a inaccessible function, while otherB->f();
is ok?
It seems you can happily access protected functions of another (same
type) - but not via a base class pointer.... I've checked the FAQs,
Meyers etc but nothing obvious I can find explains it.
|
by: t |
last post by:
Lippman's C++ Primer, 4th ed., p562, dicussion of protected members
seems to be wrong, unless I am misinterpreting things. He says:
"A derived class object may access the protected members of its base
class only through a derived object. The derived class has no special
access to the protected members of base type objects."
He gives the following example:
=========================================================
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I met with a strange issue that derived class function can not access base
class's protected member. Do you know why?
Here is the error message and code.
error C2248: 'base::~base' : cannot access protected member declared in
|
by: Mayur H Chauhan |
last post by:
All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.
Protected Class Book
End Class
Even same for...
|
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |