Class object definitions are summarized way

// ConsoleApplication34.cpp: custom console application entry point.
//
 
#include " the stdafx.h " 
#include <the iostream>
 the using  namespace STD;

class A
{
public:
    int a;
     A();
};
A::A()
{
    cout << "hello world!" << endl;
}

class CExample {
private:
    int a;
public:
    CExample ()   // Once defined through a custom constructor below the default constructor does not exist. It is necessary to re-write it again 
    {
        a = 1;
        cout << "hello " << endl;
    }
    CExample(int b)
    {
        a = b;
    }
    CExample(const CExample&  b)
    {
        a = b.a;
    }
    void Show()
    {
        cout << a << endl;
    }
};

int main ()
{
    // not new is defined on the stack. 
    CExample test1;
    CExample test2 = CExample();

    // new new heap is defined. Use released delete memory
     // parentheses represent the argument constructor call containing CExample (B int) 
     // without brackets represents the non-argument constructor call CExample () = Test3 * equivalent to new new CExample CExample ()   
    CExample * = Test3 new new CExample ( 2 );
    CExample* test4 = new CExample;

    // copy constructor series 
    CExample Test5 * ( new new CExample ( . 6 ));
    CExample test7(test1);

    Test6 CExample (); // error 
    return  0 ;
}

 

 

Several commonly given above defined methods and differences.

note:

1. There is no new new heap is on the stack, new and delete with use.

2. Under the new situation, the role of the brackets is to specify the constructor. Ie without brackets using the default constructor

 =============================================================================================

For more on the difference between heap and stack defined:

from:http://blog.sina.com.cn/s/blog_586b6c050100dhjg.html

In C ++, there are two ways to create objects:

method one:

ClassName object(param);

This specifies a type of object ClassName objects, C ++ will allocate enough storage space for all members of the object as it is stored.

Note: To save storage space, C ++ objects are created only allocate space for your data members and member functions defined in the class were assigned to a common area storage space, shared by all objects of that class.

For example, I define a class like this:

class Rec
{
   public:
   Rec(int width,int height);
   ~Rec();
   int getArea();

   private:
   int Rwidth;
   int Rheight;
};

When you Rec myRec (5,5); This creates a myRec object, and then print out the sizeof (myRec); the time will be 8 results.

MyRec Because there are two data members of type int, int a member of four bytes, so that 8 bytes myRec objects.

Objects created in this way, the memory allocation is allocated to the stack, created and destroyed by the C ++ default, automatically call the constructor and destructor

Note: When an object calls the class method which creates must be used, do not use "->" as myRec.getArea (); ".".

 

Method Two:

ClassName *object=new ClassName(param);

delete object;

This method is somewhat similar with the java, it is the same, they are allocated on the heap memory to create an object (and different); except that, C ++ objects are created returns an object pointer with new, object points to a ClassName object, C ++ object is only assigned to the pointer value of the storage space. Moreover, the object with the new dynamic created to undo must delete the object. Only delete objects will call its destructor.

Note: The object is not to create a new "*" or access the member functions of the object, but with the operator "->"; "."

For example: Rec * rec = new Rec (3,4);

      rec->getArea();

      delete rec;

by the way:

In general, the compiler memory is divided into three parts: a static memory area, stack, heap. The main static storage area to save global variables and static variables, variables call stack storage-related functions, such as address, heap memory dynamically generated variables. In c, refers to malloc, free storage space to produce the release operation, in c ++ refers to the storage area of ​​the new and delete operators act operation.

Further advantages of both methods collected:

out of the new heap, defined directly on the stack, the stack size is limited

new benefits:

1, when needed only new (very important in complex business logic and permissions system)
2, the object reliability check (no stack space limitations)
3, moderately retained control object

 =============================================================================================

new in parentheses Summary:

from:https://blog.csdn.net/wang13342322203/article/details/80807904

error: no appropriate default constructor available, are generally the problem

 

We divided into two brackets, the brackets [], or parentheses (). 
For convenience of description, is defined as a pointer type class test, pointer
test * ptest;

a situation in parentheses.
Brackets are used to indicate the application is an object or set of objects.
1 no brackets, i.e. ptest = new test; ptest obtained at this time is an object * ptest, using Delete ptest release;
2 with brackets, there is a need for a positive integer argument in parentheses.
Test = new new PTEST [N];
N can be a variable, or may be constant.
At this point the application to a space ptest is N successive objects ptest [0], ptest [1 ] ... ptest [N-1], i.e. can be used as an array.
You need to call delete [] ptest release;

Second, the situation parentheses.
Parentheses are used to specify initialization parameter, when a class pointer, which is designated to call the constructor .
When there is a parenthesis, the parentheses may be several parameters, the parameters can not.
For example
ptest = new test (); configured so that when the constructor with no arguments is called Test ();
PTEST Test new new = (. 1); will call the constructor test (int), i.e. an integer argument constructor .

2 no parentheses, call the default constructor with no arguments.
I.e.
ptest = new test;
And ptest = new test (); is the same.
================================================== ================================================== =======================

 

Guess you like

Origin www.cnblogs.com/xuhongfei0021/p/12519560.html