-3

Possible Duplicate:
What are access specifiers? Should I inherit with private, protected or public?

While going through the Qt API documentation, I came across protected types. What are protected types in C++?

I found an example of protected types mentioned in the QT documentation.

Community
  • 1
  • 1
Gandalf
  • 1
  • 29
  • 94
  • 165
  • 1
    See this example on qt http://doc.qt.nokia.com/latest/qabstractitemview.html#protected-types – Gandalf Jun 26 '11 at 08:01
  • @Als: This question is not directly related to the question of *how* to inherit, though the answer also answer this question. – Xeo Jun 26 '11 at 08:05
  • @Xeo: Yeah Second dup on the same C++-FAQ Since morning ;) the title of the FAQ doesn't reflect that it answers this Q though..I know since i answered that other Q. Anyhow copy pasted the content here.... – Alok Save Jun 26 '11 at 08:06
  • @Als: If the current question title doesn't correctly reflect the content, edit it! ;) – Xeo Jun 26 '11 at 08:11

4 Answers4

4

"Protected types" are nested typedefs and nested classes and templates and enums (i.e. nested types) that are protected, that is, accessible only in scope of the member functions of the class and member functions of derived classes (and friends, of course).

class A
{
    private:
        typedef int Int_Private;
    protected:
        typedef int Int_Protected;
    public:
        typedef int Int_Public; 
    void member() 
    {  
         Int_Private i1; //OK
         Int_Protected i2; //OK    
         Int_Public i3; //OK
    }
};

class B:A
{
    void derived_member()
    {
         Int_Private i1; //ERROR
         Int_Protected i2; //OK    
         Int_Public i3; //OK

    }
};

void someFunction()
{
         A::Int_Private i1; //ERROR
         A::Int_Protected i2; //ERROR    
         A::Int_Public i3; //OK
}
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2

There are 3 access specifiers for a class/struct/Union in C++. These access specifiers define how the members of the class can be accessed. Of course, any member of a class is accessible within that class(Inside any member function of that same class). Moving ahead to type of access specifiers, they are:
Public - The members declared as Public are accessible from outside the Class through an object of the class.
Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it.
Private - These members are only accessible from within the class. No outside Access is allowed.

An Source Code Example:

class MyClass
{
    public:
        int a;
    protected:
        int b;
    private:
        int c;
};

int main()
{
    MyClass obj;
    obj.a = 10;     //Allowed
    obj.b = 20;     //Not Allowed, gives compiler error
    obj.c = 30;     //Not Allowed, gives compiler error
}

What is a protected type?
A Protected type is a type that's defined within the scope of a protected access specifier.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • May i ask why the downvote? The reason i ask is the same content is present in a C++ faq answer and If anything is incorrect I shall correct it. – Alok Save Jun 26 '11 at 08:10
  • In the absence of an example, we can only guess. But I'm sort of guessing the OP might've been referring to the protected keyword in reference to inheritance. BTW, this isn't a downvote. I think your answer is fine. – Omnifarious Jun 26 '11 at 08:11
  • @Omnifarious: From the linked Qt page, not exactly - though both topics blend into each other. – Xeo Jun 26 '11 at 08:12
  • @Omnifarious: I already have an c++-Faq answer marked as duplicate if that's the case. – Alok Save Jun 26 '11 at 08:12
  • 2
    I am not the downvoter, but I guess your answer covers what is access-specifier, but doesn't say a word about protected **types** – Armen Tsirunyan Jun 26 '11 at 08:12
  • Oh, I know why. You're describing the access specifier, but not a type. Which is a little bit of a split hair IMHO. But a protected type is a type that's defined within the scope of a protected access specifier. – Omnifarious Jun 26 '11 at 08:13
  • @Omnifarious: Perhaps, the downvoter has set up very high standards :) I am going to steal that golden line from your comment and add it to the answer to make it complete then :) – Alok Save Jun 26 '11 at 08:16
1

These are types - enums, classes, structs, typedefs - that are defined in the containing class as protected. Things that are defined in a class under protected (versus private or public are accessible only to classes that inherit from this class.

shoosh
  • 76,898
  • 55
  • 205
  • 325
-1

protected types are almost the same as private, but the difference is that those types can be accessed by derived classes.

legion
  • 199
  • 1
  • 4
  • 13