c ++ --- Classes and Objects (six default member function)

  • Class member function in default six
  • Constructor
  • Destructor
  • Copy constructor
  • Assignment Operator Overloading
  • Const fetch address and address-operator overloading
  • const member functions

1. class member function in default six
first look at the code below

class A{
};
int main(){
	A a;
	return 0;
}

This code is not an error, but also the normal operation, why we did not write a constructor can instantiate our class. This is because the class containing the default six member functions, including a constructor, destructor, copy constructor, assignment operator overloading, to take the address and the address const Overload operator.
Here Insert Picture Description
Six default member function has its own function

  • The constructor and destructor: primary initialization and cleanup work is completed
  • Copy assignment: copy constructor to initialize the object is created using the same objects, assignment overloading mainly to assign an object to another object
  • Take overloaded address: mainly ordinary objects and take a const object address.

2. Constructor
Constructor is to complete the initialization of our work, every time we create an object is going to be calling the constructor and lifecycle value of an object called once, the constructor format. Note that we did not return the value of the constructors.

类名(参数列表){
	函数体;
};
  • Constructor with no arguments
    the name suggests is the constructor parameter list is not a constructor, such as the following class
#include <string>
class Car{
public:
	Car(){
		
	}
private:
	std::string color;
	int price;
};
int main(){
	Car car;
	return 0;
}
  • There configuration parameters, the parameter is contained in the constructor parameter list.
#include <string>
class Car{
public:
	Car(std::string color,int price){
		
	}
private:
	std::string _color;
	int _price;
};
int main(){
	Car car("yellow",100000);
	return 0;
}

note:

  • If you do not define the class constructor, c ++ compiler automatically generates a default constructor with no arguments, once the users themselves realize the constructor will no longer be generated.
  • No-argument constructor and a full default constructor is called the default constructor, and the default constructor only one, (no-argument constructor, full-default constructor, the compiler-generated constructors think is the default member function)
  • Our custom type constructor will not do anything, but will call for custom types custom type constructor, see the following code
#include <string>
class Car{
public:
	Car(std::string color,int price){
		
	}
private:
	std::string _color;
	int _price;
};
int main(){
	Car car("yellow",100000);
	return 0;
}

The results will appear below, which shows the type we call a custom constructor.Here Insert Picture Description

3. destructor
corresponding destructor and our constructor, he would sign a two objects from birth to death, the destructor is to complete the destruction of the object to complete the clean-up resources in class.
format:

~类名(){
};

Destructor features:

  • Destructor name is the class name plus ~
  • Parameters none Return value None
  • And a class has only one destructor, if not explicitly defined automatically generated
  • The end of the life cycle of an object is called automatically when

note:

In the destructor we will be divided into custom type and built-in types, when our built-in types when destructor does not do anything, but for custom types custom types of calls destructor, such as the following code

#include <iostream>
#include <string>
class Pro{
public:
	Pro(){
		std::cout << "Pro()" << std::endl;
	}
	~Pro(){
		std::cout << "~Pro" << std::endl;
	}
private:
	std::string name;
	std::string address;
};
class Car{
public:
	Car(std::string color,int price){
		
	}
private:
	std::string _color;
	int _price;
	Pro p;
};
void test(){
	Car car("yellow", 100000);
}
int main(){
	test();
	system("pause");
	return 0;
}

Here Insert Picture Description

  • For the application heap space or file descriptor, or the socket and so on must be recycled in the destructor on or off, these resources are limited, manual release will not always exist, we know that the program is running End.

4. The copy constructor
copy constructor is a constructor, and he has no configuration parameters configuration parameters except that he is using to initialize an object to another object, usually only one parameter, this is a reference to an object of class type
format:

类名(const 类& 对象)
{
}
  • Here we must add a reference to an object, when we do not add the referenced object in the error vs directly,
    Here Insert Picture Description
    the principle is that we call the copy constructor is not the time plus & when we call the function, shape parameter argument a copy, then the copy will appear in the copy when the parameter configuration, the cycle has been so, the program crashes, so the vs being given directly. Lead to infinite recursion.
    When the default copy constructor copy endian value (also referred to as a shallow copy)

  • In the following we did not declare a copy constructor when the compiler will automatically generate a default copy constructor, copy constructor but this time it does not apply to cases where there have been times when we heap space applications, such as code, see if there are any problems

#include <iostream>
#include <string>
class Pro{
public:
	Pro(){
		std::cout << "Pro()" << std::endl;
		name = (char*)malloc(1024);
	}
	~Pro(){
		std::cout << "~Pro" << std::endl;
		free(name);
	}
private:
	char* name;
	std::string address;
};

The above code should look no problems, but when we make a copy of it constructed, simply copy the value,
Here Insert Picture Description
at this time when it should be wrong, because after we apply for a copy constructor and a value of copy to b, so a and b char * name is directed with a memory, when we call the destructor will appear twice this memory is released. At this time you should use a deep copy , we are back again deep copy detailed explanation.

The assignment operator overloading
assignment operator overloading is to readability, we have two such classes assigned time if not write assignment operator overloading, then we must use a function to implement our assignment, a function call when read in sex without us "=" high, so c ++ implements operator overloading, when we do not achieve it by default generated. Operator overloading date in achieving our understanding of the class when they are over, we'll summarize here
the function prototype: type operator operator returns the value (parameter list)

  • You can not connect to other symbols to create a new operator, such as @ # ¥ etc.
  • Overload operator must have a class operand type or an enumeration type
  • Built-type operator is not changed meanings: + shaping such as built.
  • Overloaded functions as members of a class parameter is looked less than the number of operands an operator member function has a default the this parameter, the first parameter is defined as
  • . * :: sizeof :? ** ** operator is not overloaded.

Realize assignment operator overloading

#include <iostream>
#include <vector>
class car{

	car& operator=(const car& c){
		if (&c == this){
			return *this;
		}
		this->name = c.name;
		this->price = c.price;
		return *this;
	}
private:
	std::string name;
	int price;
};

Precautions assignment operator:

  • Parameter type is a reference and const, because at the time of delivery is not to modify parameters
  • The return value is checked (in the built-in type can be assigned contiguously a = b = c), not to change our original function
  • Detecting whether the assignment ourselves
  • Return * this
  • If you do not define a class assignment operator overloading, the compiler will generate a complete copy order in bytes.

6. Take const fetch address and the address of operator overloading,
taken const fetch address and the address is taken operator overloading is our this pointer is pointing to the address of the current object of our general we do not generate an address-own operator overloading, only special cases will go to reload our const, for example, we do not want to let others take our address we can rewrite as follows

#include <iostream>
#include <vector>
class car{
public:
	car& operator=(car c){
		if (&c == this){
			return *this;
		}
		this->name = c.name;
		this->price = c.price;
		return *this;
	}
	car* operator&(){
		return nullptr;
	}
private:
	std::string name;
	int price;
};
int main(){
	car a;
	std::cout << &a << std::endl;
	system("pause");
	return 0;
}

Would never have been taken address is NULL;

7. const member functions modify
the const modified class member function call const member functions, this is actually a modified pointer, indicating that any member of the class can not modify the member function
Here Insert Picture Description
at this time found that we were the team this point to name modify the time being given directly, so const member functions can not be modified to modify the contents of this pointer

Guess you like

Origin blog.csdn.net/boke_fengwei/article/details/90440917