代码之家  ›  专栏  ›  技术社区  ›  vinayvasyani

C++中的默认关键字等价?

  •  7
  • vinayvasyani  · 技术社区  · 16 年前

    我知道你可以用 default keyword 为值类型指定默认值为0,为引用类型指定空值,为结构类型指定单个成员。据我所知,C++中没有默认值。在C++编程时,你会用什么方法获得与泛型的默认关键字相同的功能呢?

    7 回复  |  直到 16 年前
        1
  •  11
  •   James McNellis    16 年前

    假设类型是默认可构造的,则可以使用值初始化。例如,

    template <typename T>
    T get()
    {
        return T(); // returns a value-initialized object of type T
    }
    

    如果类型不是默认可构造的,通常需要提供要使用的默认值。例如,

    template <typename T>
    T get(const T& default_value = T())
    {
        return default_value;
    }
    

    如果类型是默认可构造的,则可以不带参数调用此函数。对于其他类型,可以提供要返回的值。

        2
  •  2
  •   Stack Overflow is garbage    16 年前

    C++没有 default 关键字,因为它不区分引用类型和值类型。在C++中,所有类型都是C.*将考虑的值类型,并且如果它们是默认可构造的(如内置类型、POD结构和默认构造函数的类类型),则使用值初始化(默认构造函数语法)初始化它们,如James McNellis所示:(并且在这里无耻复制)

    template <typename T>
    T get()
    {
        return T(); // returns a value-initialized object of type T
    }
    

    如果 T 有一个默认的构造函数,它被调用。如果它没有构造函数,则所有内容都初始化为零/空。

    如果类型不是默认的可构造类型,那么 是 无法提供对象的默认值。

        3
  •  0
  •   Brendan Long    16 年前

    使用默认的构造函数,即使不使用 () 最后:

    #include <iostream>
    class MyClass {
        public:
        int x;
        MyClass(){
            x = 5;
        }
    };
    
    int main(){
        MyClass y;
        std::cout << y.x;
    }
    
        4
  •  0
  •   Alexander Rafferty    16 年前

    在C++中,将默认值赋值给全局范围或函数中的变量是简单的:

    int myint=2;
    float* pfloat=NULL;
    

    对于类成员,需要在类的构造函数中初始化这些成员:

    class myclass {
    private:
      int i;
    public:
      myclass() {
        i = 4;
      }
    };
    

    我不确定结构。

        5
  •  0
  •   jamesdlin    16 年前

    在C++中,通常调用 违约 构造函数(可以不带参数调用的构造函数)。这也适用于基本类型(即, int() 是0, int*() 是空指针等)。

    例如,在模板函数中,您将编写如下内容:

    template<typename T>
    T foo()
    {
        T x = T(); // See notes below about this.
    
        // Do other stuff.
    
        return x;
    }
    

    注意 T x; 本身就足以隐式调用非- POD 类型,但对于原始标量类型(例如 int )在那里它将被初始化为垃圾。( T x() 也不起作用; that would be interpreted as a function declaration )

        6
  •  0
  •   Chubsdad    16 年前

    这里是C++ 03中初始化技术的快速总结。

    初始化t类型的对象为零意味着: _ 转换为T;

    — if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
    
    — if T is a union type, the object’s first named data member89) is zero-initialized;
    
    — if T is an array type, each element is zero-initialized;
    
    — if T is a reference type, no initialization is performed.
    

    默认情况下,初始化t类型的对象意味着:

    — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    
    — if T is an array type, each element is default-initialized;
    
    — otherwise, the object is zero-initialized.
    

    要对t类型的对象进行值初始化,意味着: gt;

    — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    
    — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
    
    — if T is an array type, then each element is value-initialized;
    
    — otherwise, the object is zero-initialized
    

    有了这样的理解

    struct S{
       int x;
       S():x(1){}
    };
    
    S sg;                    // Every object of static storage duration is zero initialized at program startup before any other initialization takes place. So 'x' is initialized to 0, before the default constructor runs and sets 'x' to 1.
    
    int main{
       int x = int();        // value initialization syntax, as per rules above is initialized to 0.
    
        S sl;                // default initialized
    }
    
        7
  •  0
  •   Paul Michalik    16 年前

    这是个好问题。C++型变异的PeltoRA使书写的安全模板低于疼痛。

    template <typename T> struct FrameworkTemplate {
     T mInstance;
    };
    

    假设在理论上,用户可以将类模板实例化为

    // assume A is a known default constructible type
    FrameworkTemplate<A>
    FrameworkTemplate<const A>
    FrameworkTemplate<A const *>
    FrameworkTemplate<A const &> // and so on
    

    其中最后三个不是默认的可构造的,尽管a可能是。这就是为什么有用的泛型类型 any , nullable , lazy 等等,虽然简单直观,乍看起来,在C++中实现(安全)是微不足道的。