C++中与C#的default关键字等效的是什么?

7

在C#中,我知道你可以使用default关键字将默认值分配为0到值类型和null到引用类型,对于结构类型,各个成员也会相应地被分配。据我所知,在C++中没有默认值。在C++编程时,您会采取什么方法来获得与默认关键字相同的泛型功能?

7个回答

11
假设类型可以进行默认构造,您可以使用值初始化。例如:
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

C++没有default关键字,因为它没有引用类型和值类型之间的区别。在C++中,所有类型都是C#所考虑的值类型,如果它们是默认构造的(如内置类型、POD结构和带有默认构造函数的类类型),则使用值初始化(默认构造函数语法)进行初始化,就像James McNellis展示的那样:(并在此处无耻地复制)

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

如果 T 有默认构造函数,则会调用它。如果没有构造函数,则所有内容都将初始化为零/空值。
如果该类型不可默认构造,则对象无法赋予默认值。

只是提供信息,这个注释现在已经过时了:https://en.cppreference.com/w/cpp/keyword/default - Tarun Uday

0

使用默认构造函数,即使您在末尾省略了()也会被使用:

#include <iostream>
class MyClass {
    public:
    int x;
    MyClass(){
        x = 5;
    }
};

int main(){
    MyClass y;
    std::cout << y.x;
}

0
在C++中,为全局范围或函数中的变量分配默认值就像这样简单:
int myint=2;
float* pfloat=NULL;

对于类成员,您需要在类的构造函数中进行初始化:

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

我对结构体不太确定。


2
在C++中,结构体和类除了默认可见性之外完全相同。 - Brendan Long

0

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

To zero-initialize an object of type T means: — if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to 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 的对象进行值初始化的意思是: >
— 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
}

0
在C++中,通常会调用默认构造函数(可以不带参数调用的构造函数)。这对于原始类型也适用(即int()是0,int*()是空指针等)。
例如,在您的模板函数中,您可以编写以下内容:
template<typename T>
T foo()
{
    T x = T(); // See notes below about this.

    // Do other stuff.

    return x;
}

请注意,对于非POD类型,T x;本身就足以隐式调用默认构造函数,但对于原始标量类型(如int),它将被初始化为垃圾值。(T x()也不起作用;那将被解释为函数声明。)

0

这是一个好问题。C++类型的众多变化使得编写下面这种安全模板变得很痛苦。

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 可以。这就是为什么像 anynullablelazy 等有用的通用类型,虽然乍一看简单直观,但在 c++ 中实现起来并不容易(安全)...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接