Is C # the default keyword equivalent in C ++?

In C #, I know that you can use the default keyword to assign default values ​​as 0 for value types and null for reference types, and for struct types of individual members are assigned accordingly. As far as I know, in C ++ there are no default values. What approach would you take to get the same functionality as the default keyword for Generics while programming in C ++?

+7
c ++ c # default
source share
7 answers

Assuming the default type is constructive, you can use value initialization. For example,

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

If the type is not constructive by default, you usually need to specify a default value. For example,

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

This function can be called without an argument if the default type is constructive. For other types, you can specify a return value.

+11
source share

C ++ does not have the default keyword, since it does not distinguish between reference and values. In C ++, all types are what C # will consider value types, and if they are constructive by default (like built-in types, POD structures, and class types with default constructors), they are initialized using value initialization (default constructor syntax ), as James McNellis showed: (and shamelessly copied here)

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

If T has a default constructor, it is called. If it has no constructors, everything is initialized to zero / zero.

And if the type is not constructive by default, the default value cannot be set.

+2
source share

Use the default constructor, it was used even if you omit () at the end:

 #include <iostream> class MyClass { public: int x; MyClass(){ x = 5; } }; int main(){ MyClass y; std::cout << yx; } 
0
source share

In C ++, assigning a default value to a variable in the global scope or function is as simple as:

 int myint=2; float* pfloat=NULL; 

For class members, you need to initialize them in the class constructor:

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

I am not sure about the structures.

0
source share

Here is a brief description of the initialization methods in C ++ 03.

For zero initialization of an object of type T means: - if T is a scalar type (3.9), the object is set to 0 (zero) is 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. 

To initialize an object of type T by default:

 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. 

To initialize an object of type T means:>

 — 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 

With understanding

 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
source share

In C ++, you usually call the default constructor (a constructor that can be called without arguments). This also works for primitive types (i.e. int() is 0, int*() is a null pointer, etc.).

For example, in your template function, you should write something like:

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

Note that T x; by itself will be sufficient to implicitly call the default constructor for types , which will be interpreted as a function declaration .)

0
source share

This is a good question. In pletora C ++ types, options record safe patterns of this kind, like pain below.

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

Note that in theory, a user can instantiate your class template as

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

where the last three are not constructive by default, although it may be A. That's why useful general types like any , nullable , lazy , etc., although simple and intuitive at first glance, are not trivial to implement (safely) in C + + ...

0
source share

All Articles