简体   繁体   English

C ++类实例化理论

[英]C++ class instantiation theory

#include <iostream>
using namespace std;

class A {
public:
    A() {
        cout << "Default Ctor" << endl;
    }
};

int main() {
    A(); // <------- Problem

    return 0;
}

It shows Default Ctor on console. 它在控制台上显示Default Ctor My questions 我的问题

  • Is is valid? 有效吗?
  • If so, how did it instantiate since I didn't use new or any object? 如果是这样,它是如何实例化的,因为我没有使用新的或任何对象?

You are creating a new object with A() . 正在使用A()创建一个新对象。

Is is valid? 有效吗?

Yes it is. 是的。

If so, how did it instantiate since I didn't use new or any object? 如果是这样,它是如何实例化的,因为我没有使用新的或任何对象?

new simply creates the object in dynamic memory. new只是在动态内存中创建对象。 You're creating the object in automatic memory. 您正在自动内存中创建对象。 Also, just because you didn't give the object a name, doesn't mean it isn't created. 另外,仅仅因为你没有给对象命名,并不意味着它没有被创建。

Think of this: 想一想:

int foo() { return 1; }

int main()
{
   foo();
}

Leaving optimizations aside Did foo() actually return 1 ? 抛弃优化 foo()实际上返回1吗? Yes it did. 是的,它确实。 Just that you're not using it. 只是你没有使用它。

EDIT: 编辑:

Let's break it down a bit: 让我们分解一下:

A();  //creates a temporary unnamed object in automatic storage

A a;   //creates an object a of type A in automatic storage

A a(); //creates no object, but declare a function a returning A and taking no parameters

A a(A());   //these two are equivalent
A a = A();  //creates a temporary unnamed object and creates an object a in automatic storage
            //using the compiler-generated copy constructor

A a;
a = A();    //creates an object a in automatic storage
            //creates an unnamed temporary object and uses the compiler-generated assignment operator
            //to assign it to a

A a = &A(); //invalid, &A() is the address of a temporary a, which is illegal

Is is valid? 有效吗?
Yes, It is valid 是的,这是有效的

What exactly happens? 究竟发生了什么?

A(); 

Creates a temporary nameless object of the type A by calling its default no argument constructor but the object is destructed by the time the next statement is executed since. 通过调用其默认的无参数构造函数来创建类型A的临时无名对象,但该对象在下一个语句执行后被破坏。

If so, how did it instantiate since I didn't use new or any object? 如果是这样,它是如何实例化的,因为我没有使用新的或任何对象?
You can create objects on the local/automatic storage or on dynamic storage depending on your usage. 您可以根据用途在本地/自动存储或动态存储上创建对象。

When you use new objects are created on dynamic storage(heap), when you create a object as you have it is created on the local/automatic storage(stack). 在动态存储(堆)上创建使用new对象时,在创建对象时,会在本地/自动存储(堆栈)上创建对象。

So using new only determines where the object will be created not whether it will be created. 因此,使用new仅确定创建对象的位置 ,而不是是否将创建对象。

What are Temporary Nameless objects? 什么是临时无名对象?
You do not always need to name an object to instantiate them. 您并不总是需要命名对象来实例化它们。
For Ex: 对于Ex:
While calling function, when you pass objects by value temporary nameless objects are created and automatically destroyed all the time.These are objects which do not have any name and hence cannot be explicitly referred through program but they do serve the purpose for which they were created. 在调用函数时,当您按值传递对象时, 临时无名对象会被创建并自动销毁。这些对象没有任何名称,因此不能通过程序显式引用,但它们的确有助于创建它们的目的。

In simple words, 简单来说,
You are creating a nameless temporary object on the local/automatic storage which does not exist once the statement completes execution. 您正在本地/自动存储上创建无名临时对象,该语句在语句完成执行后不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM