内部类和外部成员的访问

发布于 2024-08-29 06:02:46 字数 1571 浏览 4 评论 0原文

我一直认为内部类可以访问其外部类中的所有数据,但有代码:

template<class T>
class Vector
{
 template<class T>
 friend
std::ostream& operator<<(std::ostream& out, const Vector<T>& obj);
private:
 T** myData_;
 std::size_t myIndex_;
 std::size_t mySize_;
public: 
 Vector():myData_(nullptr),
  myIndex_(0),
  mySize_(0)
 { }
 Vector(const Vector<T>& pattern);
 void insert(const T&);
 Vector<T> makeUnion(const Vector<T>&)const;
 Vector<T> makeIntersection(const Vector<T>&)const;
 class Iterator : public std::iterator<std::bidirectional_iterator_tag,T>
 {
 private:
  T** itData_;
 public:
  Iterator()//<<<<<<<<<<<<<------------COMMENT
  { /*HERE I'M TRYING TO USE ANY MEMBER FROM Vector<T> AND I'M GETTING ERR SAYING:
   ILLEGAL CALL OF NON-STATIC MEMBER FUNCTION*/}

  Iterator(T** ty)
  { 
   itData_ = ty;
  }

  Iterator operator++()
  {
   return ++itData_;
  }

  T operator*()
  {
   return *itData_[0];
  }

  bool operator==(const Iterator& obj)
  {
   return *itData_ == *obj.itData_;
  }

  bool operator!=(const Iterator& obj)
  {
   return *itData_ != *obj.itData_;
  }

  bool operator<(const Iterator& obj)
  {
   return *itData_ < *obj.itData_;
  }
 };

 typedef Iterator iterator;

 iterator begin()const
 {
  assert(mySize_ > 0);
  return myData_;
 }

 iterator end()const
 {
  return myData_ + myIndex_;
 }
};

请参阅标记为 COMMENT 的行。
那么我可以在内部类中使用外部类的成员吗?
不用担心命名,它不是向量,而是集合。
谢谢。

I always thought that internal class has access to all data in its external class but having code:

template<class T>
class Vector
{
 template<class T>
 friend
std::ostream& operator<<(std::ostream& out, const Vector<T>& obj);
private:
 T** myData_;
 std::size_t myIndex_;
 std::size_t mySize_;
public: 
 Vector():myData_(nullptr),
  myIndex_(0),
  mySize_(0)
 { }
 Vector(const Vector<T>& pattern);
 void insert(const T&);
 Vector<T> makeUnion(const Vector<T>&)const;
 Vector<T> makeIntersection(const Vector<T>&)const;
 class Iterator : public std::iterator<std::bidirectional_iterator_tag,T>
 {
 private:
  T** itData_;
 public:
  Iterator()//<<<<<<<<<<<<<------------COMMENT
  { /*HERE I'M TRYING TO USE ANY MEMBER FROM Vector<T> AND I'M GETTING ERR SAYING:
   ILLEGAL CALL OF NON-STATIC MEMBER FUNCTION*/}

  Iterator(T** ty)
  { 
   itData_ = ty;
  }

  Iterator operator++()
  {
   return ++itData_;
  }

  T operator*()
  {
   return *itData_[0];
  }

  bool operator==(const Iterator& obj)
  {
   return *itData_ == *obj.itData_;
  }

  bool operator!=(const Iterator& obj)
  {
   return *itData_ != *obj.itData_;
  }

  bool operator<(const Iterator& obj)
  {
   return *itData_ < *obj.itData_;
  }
 };

 typedef Iterator iterator;

 iterator begin()const
 {
  assert(mySize_ > 0);
  return myData_;
 }

 iterator end()const
 {
  return myData_ + myIndex_;
 }
};

See line marked as COMMENT.
So can I or I can't use members from external class while in internal class?
Don't bother about naming, it's not a Vector it's a Set.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-09-05 06:02:46

您需要将外部类的实例传递给内部类。换句话说,您的 Iterator 类必须有一个指向 Vector 实例的引用(或指针)。执行此操作的最佳方法是让 Iterator 构造函数获取对 Vector 的引用。

Iterator(Vector& v) : vec_(v)
{
  vec_.do_something();
}

You need to pass an instance of the external class to the internal class. In other words, your Iterator class must have a reference (or pointer) to an instance of Vector handy. The best way to do this is to have the Iterator constructor take a reference to a Vector.

Iterator(Vector& v) : vec_(v)
{
  vec_.do_something();
}
不乱于心 2024-09-05 06:02:46

您无法从 C++ 中的周围类访问字段或方法!

You cannot access fields or methods from the sourrounding class in C++!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文