C ++模板+变量默认值 [英] C++ templates + variable default values

查看:54
本文介绍了C ++模板+变量默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决这个问题,谷歌先生似乎也不太了解它,或者我只是没有正确地查询,可能是后者,但我有一个模板;



 template< class T>  class  CSmartArray 
{
public
CSmartArray(... );

blah ... blah ... blah

private
T& m_preset_value; // < ===要设置的值
};

模板< class T> T&安培; CSmartArray< T> CSmartArray(...)
{
// 如何设置跟随某个默认值的值如零?
m_preset_value = 0 ; // Nope
m_preset_value =(T) 0 ; // Nope
m_preset_value =(T&) 0 ; // Nope
}





问题是如何将m_preset_value设置为某个默认值,例如零?



它可能非常简单但是没有看到它但是如何设置m_preset_value的任何值?



紧急发送代码plz ...... :)



谢谢你们所有



BTW使用GCC的模板是PITA!

解决方案

这个为我工作(在VS中):



 template< class T>  class  CSmartArray 
{
public
CSmartArray();

private
const T& m_preset_value;
};

模板< class T> CSmartArray< T> :: CSmartArray():m_preset_value( 0
{

}

< br $> b $ b

不确定它是不是你想要的。 (注意我必须使它成为一个const引用。你可能只想在这里使用指针,它可能会更容易。)


如果我们看一下生成的代码然后引用与指针完全相同。在语言层面上,语法引用有更严格的规则:引用必须在声明/定义它时恰好初始化一次。如果引用是类的成员变量,那么您必须从类的每个构造函数的初始化列表中初始化它。以后不能更改引用以指向其他内容。在声明和初始化之后,引用变量的标识符就像它是引用所指向的对象的标识符一样工作。因此,将引用初始化为零没有意义。如果你想在初始化之后将引用更改为指向其他内容,那么你想要使用指针而不是引用。


将引用成员添加到一个引用成员没有多大意义。 未知类型。如果这是你想要做的(假设存在默认构造函数),你还必须实现析构函数并确保该对象不可复制...



如果没有,那么你必须确保类型是POD类型(规则与C ++ 11略有不同,但因为我不知道所有细节)。



因此,你会有这样的东西(为简单起见,所有函数都是内联定义的)

 template< class T>  class  CSmartArray 
{
public
CSmartArray():m_preset_value (* new T())
{
}

~CSmartArray()
{
delete & m_preset_value;
}

private
CSmartArray( const CSmartArray&); // 无副本
CSmartArray& operator =( const CSmartArray&); // 无副本

T& m_preset_value;
};





如果使用了一个实例,那么它会简化为(这是我建议的要做的事情)

模板< class T>  class  CSmartArray 
{
public
CSmartArray():m_preset_value () // 在这种情况下,Empty()将强制使用默认值,因为很长时间。
{
}

private
T m_preset_value;
};





顺便说一句,这不是很灵活,因为无法使用默认值创建对象构造


I'm having trouble wrapping my head around this and Mr. Google doesn't seem to know much about it either or I'm just not querying correctly, probably he latter but I've got a template;

template<class T> class CSmartArray
{
   public:
     CSmartArray(...);

      blah...blah...blah

   private:
      T& m_preset_value;  //<=== The value to set
};

template <class T> T& CSmartArray<T>CSmartArray(...)
{
   //How do I set the following value to some default value such as zero?
   m_preset_value = 0;     //Nope
   m_preset_value = (T)0;  //Nope
   m_preset_value = (T&)0; //Nope
}



Question is how do I set the m_preset_value to some default value such as zero?

It's probably very simple but am not seeing it but how do I set any value to m_preset_value?

Urgent send code plz...... :)

Thanks Y'all

BTW Templates using GCC is a PITA!

解决方案

This worked for me (in VS):

template<class T> class CSmartArray
{
   public:
     CSmartArray();

   private:
      const T& m_preset_value;
};

template <class T> CSmartArray<T>::CSmartArray() : m_preset_value(0)
{
   
}



Not sure if it's quite what you want though. (Note I had to make it a const reference. You might just want to use a pointer here instead, it will probably be easier.)


If we take a look at the generated code then a reference is exactly the same as a pointer. On language level syntactically references have more strict rules: a reference has to be initialized exactly once where you declare/define it. If the reference is the member variable of a class then you have to initialize it from the initializer list of every constructor of the class. A reference can not be changed later to point to something else. After declaration and initialization the identifier of the reference variable works as if it was the identifier of the object to which the reference points. For this reason initializing a reference to zero has no point. If you want to change the reference after initialization to point to something else then you want to use a pointer instead of a reference.


It does not make much sense to have a reference member to an "unknown" type. If this is really what you want to do (assuming that a default constructor exists), you would also have to implement the destructor and ensure that the object is not copyable...

If not, then you have to ensure that the type is a POD type (rules are a bit different with C++ 11 but since I don't know all details).

Thus, you would have something like this (for simplicity all functions are defined inline):

template<class T> class CSmartArray
{
public:
  CSmartArray() : m_preset_value(*new T())
  {
  }

  ~CSmartArray()
  {
    delete &m_preset_value;
  }

private:
  CSmartArray(const CSmartArray &); // No copy
  CSmartArray& operator=(const CSmartArray &); // No copy

  T& m_preset_value;
};



If an instance is used, then it simplify to this (this is what I would recommand to do):

template<class T> class CSmartArray
{
public:
  CSmartArray() : m_preset_value() // Empty () will force default value in that case since a long time.
  {
  }

private:
  T m_preset_value;
};



By the way, this is not very flexible as it is not possible to create object not using the default constructor.


这篇关于C ++模板+变量默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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