在C ++中的派生类构造函数中访问基本模板类中的变量 [英] Accessing variables from base template class in derived class constructor in C++

查看:146
本文介绍了在C ++中的派生类构造函数中访问基本模板类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看这个简单的代码示例,包括一个基类和一个派生自Base的类,它需要在其构造函数中使用基类成员的地址。

Let's look at this simple code sample including a base class and a class derived from Base, which needs the address of a base class member in its constructor.

#include <vector>
#include <inttypes.h>
#include <stdio.h>

class Base
{
protected:
  std::vector<uint32_t> arr;
public:
  Base(std::vector<uint32_t> arr_in): arr(arr_in) {}
};

class Derived: public Base
{
private:
  uint32_t *parr;
public:
  Derived(std::vector<uint32_t> arr_in): Base(arr_in)
  {
    parr = &arr[0];
  }

  uint32_t *get_parr();
};

uint32_t *Derived::get_parr(void)
{
  return parr;
}

int main()
{
  std::vector<uint32_t> myarr(3, 1);
  Derived myderived(myarr);
  printf("myderived.myarr adress = %p", myderived.get_parr());
}

由于派生类的构造函数首先调用基类构造函数,然后才调用执行其代码块,已经可以访问基类的成员。所以一切正常。

Since the constructor of the derived class calls the base class constructor first and only then executes its code block, the members of the base class can already be accessed. So everything works fine.

现在我更改代码示例,以便我的两个类是模板。

Now I change the code sample so that my two classes are templates.

#include <vector>
#include <inttypes.h>
#include <stdio.h>

template<typename T>
class Base
{
protected:
  std::vector<T> arr;
public:
  Base(std::vector<T> arr_in): arr(arr_in) {}
};

template<typename T>
class Derived: public Base<T>
{
private:
  T *parr;
public:
  Derived(std::vector<T> arr_in): Base<T>(arr_in)
  {
    parr = &arr[0];
  }

  T *get_parr();
};

template<typename T>
T *Derived<T>::get_parr(void)
{
  return parr;
}

int main()
{
  std::vector<uint32_t> myarr(3, 1);
  Derived<uint32_t> myderived(myarr);
  printf("myderived.myarr adress = %p", myderived.get_parr() );
}

但是第二个示例在编译时给出了以下错误消息:

But this second sample gives me the following error message upon compiling:

class_temp.cpp: In constructor ‘Derived<T>::Derived(std::vector<T>)’:
class_temp.cpp:23:13: error: ‘arr’ was not declared in this scope
     parr = &arr[0];

那么为什么在带有模板类的第二个示例中派生类构造函数不知道基类成员?
或者我在这里做错了什么?

So why is it that in the second sample with template classes the derived class constructor doesn't know about the base class member? Or am I doing something wrong here?

谢谢。

推荐答案

arr 现在是一个从属名称。它取决于 T 。如果有一些 T Base< T> 专门没有,该怎么办? ARR ?具体来说,从[temp.dep]:

arr is a dependent name now. It depends on T. What if there is some T for which Base<T> is specialized to not have an arr? Specifically, from [temp.dep]:


在类或类模板的定义中,依赖基类的范围(14.6) .2.1)在非限定名称查找期间未检查
,无论是在类模板或成员的定义点,还是在
期间,类模板或成员的实例化。

In the definition of a class or class template, the scope of a dependent base class (14.6.2.1) is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

Base< T> 是一个从属基类 - 它取决于模板参数 T ,因此在不合格的名称查找期间不会检查其范围。解决这个问题的方法是使用限定名称查找。也就是说,类名:

Base<T> is a dependent base class - it depends on the template parameter T, so its scope is not examined during unqualified name lookup. The way around this is to use qualified name lookup. That is, either the class name:

parr = &Base<T>::arr[0];

或只需

parr = &this->arr[0];

这篇关于在C ++中的派生类构造函数中访问基本模板类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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