D
D
Daniil Demidko2016-03-08 04:37:35
C++ / C#
Daniil Demidko, 2016-03-08 04:37:35

Constructors and Equation in C Similar Languages?

I'm talking about constructors here.
For example, we have such an Example structure in C ++ code:

struct Example
{
     Example ( const int &i ) {}
};

And its counterpart in C# code:
// struct - что бы работать со стуктурой в стеке, аналогично примеру с плюсами
struct Example
{
    public Example ( System.Int32 i ){}
}

And now the fun part:
C++ :
Example a ( 1 ) ; // Создаем обьект, вызываем конструктор
Example a = 1 ; // То же самое

C# :
Example a = new Example ( 1 ) ; // Создаем обьект и приравниваем его к Example (1)

That is, we do not call the constructor directly, but through a perverted syntax with new, we equate our object 'a' with the object returned by the new Example (1). At least that's how I understood it. Did I understand correctly?
If in the pros everything after the declaration, even with equals (=) - is a constructor call, then in C # after the declaration we make a real assignment?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Stanislav Makarov, 2016-03-08
@Nipheris

Unfortunately, things are a bit more complicated, and you need to better understand what's going on in both cases.
In C++, an object's life cycle, where and how it exists is determined by the code that creates the object. In other words, the code that USES the class determines whether the variable will be created automatically (Example a(..)), or dynamic (new Example(..)). Yes, in the class code, you can restrict the code that uses it by prohibiting the use of the copy constructor and the assignment operator, but initially the design of the language implies that the decision is made not by the "creator" of the class / structure, but by its "user".
In C#, the behavior of a class/struct type variable depends primarily on the class/struct itself. Class instances, i.e. reference types are created with a dynamic life cycle, and reference counting and garbage collection work for them. Struct instances are created with an automatic life cycle, struct variables store the values ​​of the structs, not references to them.
In other words, the expression new T(...) is simply a unified syntax for creating an instance of a type, independent of the nature of that type. Whether this is convenient or not is a separate conversation, but in the current language design this is quite logical (especially in the context of generics). Likewise, T a is the unified syntax for declaring a type variable.
You can't blindly compare what's going on in C# and what's happening in C++.

D
Dmitry Kovalsky, 2016-03-08
@dmitryKovalskiy

No, you're wrong. operator = is an assignment operator and has no hidden actions in C#. The difference is that the C++ "Example a" syntax creates an object. A similar notation in C# creates a reference to an object on the stack, and whether the object itself is created or not is the concern of other instructions.
You don't call the constructor directly. new is a constructor call, not a "perverted syntax". Don't want new? for God's sake - write factories, methods for creating objects, and so on.

O
OnYourLips, 2016-03-08
@OnYourLips

For C#, it's more convenient to write like this:
No need to repeat the type.

M
maaGames, 2016-03-08
@maaGames

struct Example
{
    explicit Example ( const int &i ) {}
};

And C++ code will be equivalent to C# code. Because not "the same", but implicit type casting.

R
Roman, 2016-03-08
@yarosroman

In C++, you can also write Example a = new Example(), or you can write as you wrote, but if you don’t understand the difference in these expressions, then you should read further books. If you write Example a(1), then the variable will be destroyed when exiting the scope, and when Example a = new Example(1), only the variable-reference to the object will be destroyed, the object itself will remain in memory, while the reference to an object can be returned from a function, for example if you are writing a factory function. However, if you still write Example a(1) in the function and return the reference from the function, the object will still be destroyed, and you will get a reference to a non-existent object. C++ has several "smart pointer" classes that work similarly to C# pointers.
Plus the difference between structures and classes with C# and C++ is colossal. In C++, a structure is a class in which all members by default have a default access level of public, instead of private in classes, then in C# they are syntactically, semantically different things with different behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question