C + + face question (1-3)

Source: Internet
Author: User

Title (1)

What is the output of the code in operation? What's wrong with this piece of code?

<!-- lang: cpp -->#include <iostream>class A{    public:        A()        {            std::cout << "A is created." << std::endl;        }        ~A()        {            std::cout << "A is deleted." << std::endl;        }};class B : public A{    public:        B()        {            std::cout << "B is created." << std::endl;        }        ~B()        {            std::cout << "B is deleted." << std::endl;        }}; int _tmain(int argc, _TCHAR* argv[]){    A* pA = new B();    delete pA;    return 0;}

answer : Output three lines, respectively: A is created. B is created. A is deleted.

When you create B with new, the constructor of B is called back. When you call the constructor of B, the constructor of a is called first. So we first output a is created. B is created.

The destructor is called when the DELETE statement is next run. Since the PA is declared as a pointer to type a, and the destructor of base class A is not marked with virtual, only the destructor of a is called, and the destructor of B is not called.

Since the PA is actually a pointer to an instance of B, it only calls the destructor of base class A at the time of the destructor, but does not call the destructor of B. There is a problem. If you create some resources in type B, such as file handles, memory, and so on, you will not be released in this case, resulting in a resource leak.

It is recommended that you define a destructor as a virtual function, which avoids improper use and causes resources to not be freed.
PS: In constructors, virtual functions lose their dynamic binding properties.

Title (2)

We can modify the member function of a class with static, or you can use the const to decorate a member function of a class (written at the end of the function to indicate that the member variable cannot be modified, not the one that represents the return value as a constant). Excuse me: Can I use both static and const to modify the member functions of the class?

answer : No, you can't.

In order to ensure that the function cannot modify the state of an instance of a class when implementing a const member function, the C + + compiler adds an implicit parameter, const this*, to the function. However, when a member is static, the function does not have this pointer. This means that static usage and static are conflicting.

We can also understand this: the semantics of the two are contradictory. Static means that the function works only on static variables of a type, not with instances of the class, and that the function of const is to make sure that functions cannot modify the state of an instance of a class, and that there is no relation to a static variable of a type. Therefore, they cannot be used at the same time.

Title (3)

What is the output of the C + + code in operation?

<!-- lang: cpp -->#include <iostream>class A{private:    int n1;    int n2;public:    A(): n2(0), n1(n2 + 2)    {    }    void Print()    {            std::cout << "n1: " << n1 << ", n2: " << n2 << std::endl;    }};int _tmain(int argc, _TCHAR* argv[]){    A a;    a.Print();    return 0;}

answer : The output N1 is a random number with a n2 of 0.

In C + +, the order in which member variables are initialized is the same as the order in which they are declared in the type, regardless of their order in the constructor's initialization list. So in this problem, the N1 is initialized first, and the parameter n2 of the initial N1 is not initialized, it is a random value, so N1 is a random value. When initializing N2, it is initialized according to parameter 0, so n2=0.

Resources:

100 Questions selected by programmers

C + + face question (1-3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.