Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+的可访问性+;基类派生类中的向量_C++_Inheritance_Friend_Derived Class - Fatal编程技术网

C++ C+的可访问性+;基类派生类中的向量

C++ C+的可访问性+;基类派生类中的向量,c++,inheritance,friend,derived-class,C++,Inheritance,Friend,Derived Class,下面的示例简化了我的场景: #include <iostream> #include <vector> using namespace std; class C; class A { protected: C * cPointer; A(); virtual void updateList() = 0; void callFunc(); }; class B : public A { private: vector<

下面的示例简化了我的场景:

#include <iostream>
#include <vector>
using namespace std;
class C;
class A
{
  protected:
    C * cPointer;
    A();
    virtual void updateList() = 0;
    void callFunc();
};

class B : public A
{
  private:
    vector<int> list;
    void updateList();
  public:
    void callFromA();
};

class C
{
  friend class A;
  friend class B; // I want to get rid off this declaration
  private:
    int sum;
    void set_sum( int val );
  public:
    static C * getCPointer();
};

A::A()
{
  cPointer = C::getCPointer();
}

void A::callFunc()
{
  updateList();
}

void B::updateList()
{
  list.push_back(2);
  list.push_back(4); 
  int s = 0;
  for( unsigned int i=0; i<list.size(); i++ )
  {
    s += list[i];
  }
  cPointer->set_sum(s);
}

void B::callFromA()
{
  callFunc();
}

void C::set_sum( int val )
{
  sum = val;
  cout << "Sum at C is: " << sum << endl;
}

C * C::getCPointer()
{
  static C cPointer;
  return & cPointer;
}

int main( int argc, char ** argv)
{
  B b;
  b.callFromA();
  return 0;
}
#包括
#包括
使用名称空间std;
丙级;;
甲级
{
受保护的:
C*cPointer;
A();
虚拟void updateList()=0;
void callFunc();
};
B类:公共A
{
私人:
向量表;
void updateList();
公众:
void callFromA();
};
C类
{
A级朋友;
friend class B;//我想摆脱这个声明
私人:
整数和;
无效集和(int-val);
公众:
静态C*getCPointer();
};
A::A()
{
cPointer=C::getCPointer();
}
void A::callFunc()
{
updateList();
}
void B::updateList()
{
列表。推回(2);
列表。推回(4);
int s=0;
对于(无符号整数i=0;iset_和);
}
void B::callFromA()
{
callFunc();
}
void C::set_sum(int val)
{
sum=val;

cout我不确定我是否理解您的所有限制,但也许这对您更有效。基本上,您可以使用虚拟函数从
A
访问
B::list
。我已经对代码中的更改进行了注释

#include <iostream>
#include <vector>
using namespace std;
class A;

class C
{
    friend class A;
private:
    int sum;
    void set_sum(int val);
public:
    static C * getCPointer();
};

class A
{
protected:
    C * cPointer;
    A();
    virtual int getS() = 0;             // virtual function to calculate data from vector in derived class B
    virtual void updateList()
    {
        cPointer->set_sum(getS());      // A is friend of C, so you can access B data from A
    }
    void callFunc();
};

class B : public A
{
private:
    vector<int> list;
    void updateList();

    int getS()                          // concrete implementation to access vector data
    {
        int s = 0;
        for (unsigned int i = 0; i < list.size(); i++)
        {
            s += list[i];
        }
        return s;
    }


public:
    void callFromA();
};

A::A()
{
    cPointer = C::getCPointer();
}

void A::callFunc()
{
    updateList();
}

void B::updateList()
{
    list.push_back(2);
    list.push_back(4);
    A::updateList();                    // Call to super implementation
}

void B::callFromA()
{
    callFunc();
}

void C::set_sum(int val)
{
    sum = val;
    cout << "Sum at C is: " << sum << endl;
}

C * C::getCPointer()
{
    static C cPointer;
    return &cPointer;
}

int main(int argc, char ** argv)
{
    B b;
    b.callFromA();
    return 0;
}
#包括
#包括
使用名称空间std;
甲级;
C类
{
A级朋友;
私人:
整数和;
无效集和(int-val);
公众:
静态C*getCPointer();
};
甲级
{
受保护的:
C*cPointer;
A();
virtual int getS()=0;//从派生类B中的向量计算数据的虚拟函数
虚拟void updateList()
{
cPointer->set_sum(getS());//A是C的朋友,所以您可以从A访问B数据
}
void callFunc();
};
B类:公共A
{
私人:
向量表;
void updateList();
int getS()//访问向量数据的具体实现
{
int s=0;
for(无符号整数i=0;icout您不能访问基类内的派生类成员,句号。手头的对象可能是基类,甚至可能是完全不相关的派生类,具有保证的“有趣”连续性。任何要求这样做的设计都会被严重破坏,需要重新思考


您可以将要执行此操作的基类的成员函数设置为虚拟函数,并在派生类中重新定义它,以执行您想执行的任何扭曲操作。同时,基类的纯洁成员可以拒绝调用,以合理的方式发出错误信号。这样,您就可以保证不会发生任何异常情况。

Ar您是否从
B::callFromA()调用全局函数
callFunc()
?那么它在哪里呢?这里的问题可能是,您将有许多从基
A
派生的类,并且您不想将所有这些类都列为
C
的朋友。为了帮助实现这一点,您可以只将基类
A
设为
C
的朋友,并且可以放入一个受保护的
set\u sum()
方法进入
A
,所有派生类都可以调用该方法,而
A::set_sum
所做的一切就是将调用转发到
C::set_sum
。此转发函数调用将由编译器优化。@barakmanos:callFunc()的作用域为A,从B::callFromA()调用考虑到B是从A派生出来的。@Code Warrior:是的,我现在明白了;谢谢。@pasztorpisti:这看起来是一个简单而有效的解决方案。.谢谢!谢谢你的评论。.我不允许将其作为一个公共函数,因为这是一个内部函数,不应该向用户公开。.但是,我们只能在protec中添加一些新内容ted/私人范围!