如何访问派生类中的受保护成员? [英] How to access protected members in a derived class?

查看:347
本文介绍了如何访问派生类中的受保护成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.parashift .com / c ++ - faq-lite / basics-of-inheritance.html#faq-19.5


数据成员或成员函数)只能由该类的成员函数和朋友以及派生类的成员函数和朋友访问。


那么,在派生类中访问受保护函数 fun 的方式是什么?

  #include< iostream> 
using namespace std;

class X
{
private:
int var;
protected:
void fun()
{
var = 10;
cout<< \\\
From X< var;
}
};

class Y:public X
{
private:
int var;
public:
void fun()
{
var = 20;
cout<< \ nFrom Y< var;
}

void call()
{
fun();

X objX;
objX.fun();
}
};

这会导致:

  anisha @ linux-dopx:〜/> g ++ type.cpp 
type.cpp:在成员函数'void Y :: call()':
type.cpp:9:8:error:'void X :: fun
type.cpp:32:14:错误:在此上下文中

在派生类(C ++)中访问受保护的成员

给定:


您只能访问类型实例中的受保护成员
派生自您的类型)。您不能访问受保护的成员的
实例的父或表亲类型。



在您的情况下,Derived类只能访问b成员的
派生实例,而不是不同的Base实例。



更改构造函数以获取Derived实例也会解决
的问题。


如何在不改变构造函数声明的情况下实现?

解决方案

我认为你想要做的事情应该看起来像这样:

  #include< iostream> 
using namespace std;

class X
{
private:
int var;
protected:
virtual void fun()
{
var = 10;
cout<< \ nFrom X< var;
}
};

class Y:public X
{
private:
int var;
public:
virtual void fun()
{
var = 20;
cout<< \ nFrom Y< var;
}

void call()
{
fun();


X :: fun();
}
};

这样可以从基类中调用hiden成员。否则,您必须添加朋友X,因为它指向其他帖子。


From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5

A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes

So, what is the way to access the protected function fun in the derived class?

#include <iostream>
using namespace std;

class X
{
    private:
        int var;
    protected:
        void fun () 
        {
            var = 10;
            cout << "\nFrom X" << var; 
        }
};

class Y : public X
{
    private:
        int var;
    public:
        void fun () 
        {
            var = 20;
            cout << "\nFrom Y" << var;
        }

        void call ()
        {
            fun ();

            X objX;
            objX.fun ();
        }
};

This results in:

anisha@linux-dopx:~/> g++ type.cpp
type.cpp: In member function ‘void Y::call()’:
type.cpp:9:8: error: ‘void X::fun()’ is protected
type.cpp:32:14: error: within this context

I saw this: Accessing protected members in a derived class (C++)

Given:

You can only access protected members in instances of your type (or derived from your type). You cannot access protected members of an instance of a parent or cousin type.

In your case, the Derived class can only access the b member of a Derived instance, not of a different Base instance.

Changing the constructor to take a Derived instance will also solve the problem.

How can this be accomplished without changing the constructor declaration?

解决方案

I think that the thing you are trying to do should looks like this:

#include <iostream>
using namespace std;

class X
{
    private:
        int var;
protected:
    virtual void fun () 
    {
        var = 10;
        cout << "\nFrom X" << var; 
    }
};

class Y : public X
{
private:
    int var;
public:
    virtual void fun () 
    {
        var = 20;
        cout << "\nFrom Y" << var;
    }

    void call ()
    {
        fun ();


        X::fun ();
    }
};

That way you can invoke hiden member from your base class. Otherwise you have to add friend X as it was pointed in other post.

这篇关于如何访问派生类中的受保护成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆