C ++ Class Default Constructor

I used to ask why this is considered bad:

class Example { public: Example(void); ~Example(void); void f() {} } int main(void) { Example ex(); // <<<<<< what is it called to call it like this? return(0); } 

Now I understand that instead, a function prototype is created that returns an Example type. I still don't understand why this works in g ++ and MS VC ++, though.

My next question is to use the above, will this call be valid?

 int main(void) { Example *e = new Example(); return(0); } 

? What is the difference between this and a simple call to Example e () ??? As I know, this is a function prototype, but maybe some compilers forgive this and allow it to call the default constructor? I also tried:

 class Example { private: Example(); public: ~Example(); }; int main(void) { Example e1(); // this works Example *e1 = new Example(); // this doesn't return(0); } 

So, I'm a little confused :( Sorry if this was asked millions of times.

+1
source share
5 answers

this question will be helpful in understanding this behavior.

+1
source

It's easy, Daniel:

 Example *e = new Example(); 

This is not like the Example function, is it? The function has a return value, name and parameters. How would it be higher?

Example e1 (); // this works

Yes, because you are not creating an instance of Example anywhere. You simply indicate the code that somewhere there is a function defined in the surrounding namespace, and you might want to call that function. Yes, it is true that an instance will be created to return the example object. But this does not mean that an instance is being created at this moment. Rather, an instance is created in the function when you call it.

+5
source

Hmm ... OK:

Example e1 ();

Does not work. You might think that it is, or some kind of compiler accepts it, but it does not instantiate an example called e1, it just declares a function prototype. Remove the brackets and it does what you want.

It:

Example * e1 = new Example ();

Will not work because the constructor is private. If you make the constructor public, it will create an object on the heap, and e1 will be a pointer to this object. You will need to delete the object when you are done with it.

+1
source

For the first question, a "new example ()" would be valid. Yes, this is completely legal C ++ code. Although you must be completely correct, you will need to remove the object before returning from main (), otherwise it will result in a memory leak.

Example:

 int main(void) { Example *e = new Example(); delete e; return(0); } 

For the last question. String "Example e1 ();" is valid because it declares a function prototype. In fact, this does not lead to the execution of machine code (possibly stack space). It simply says that there is a function prototype with no arguments, returning the type of the example.

The second line, although definitely not the case. At this point, you are trying to actually execute the constructor for an example. This is not legal because the accessibility of a function is private, therefore a compiler error.

+1
source

I think you should distinguish between “these analyzes”, “compilation”, “these links” and “this works”, and try to think like a C ++ parser or compiler / linker to see that first example

 Example e1(); // function prototype 

Looks like a function declaration. The parser will understand this in this way, so you cannot call, for example. member function on e1 . The compiler will generate a symbol related to some function (it still does not see it), but since the function is not used anywhere, it will not complain. If you add this code, it will be:

 e1.f();// since e1 is a function, it has no member 'f' => compiler error 

(as a side element: this code will also compile:

 int a_function_prototype(int); // another prototype. e1(); // should work! a_function_prototype(5); 

but after the compiler finishes, the linker will start looking for the actual function bodies and will not find it.

Now since the line

 Example* e = new Example(); 

contains the keyword new , which the compiler recognizes, and he knows that it can only be found in the distribution + construction of a new object, it will generate code for this.

0
source

Source: https://habr.com/ru/post/1333810/


All Articles