Error[8]: Undefined offset: 40, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如果类包含指针?

Copy pointer:

#ifndef PERSON_H_
#define PERSON_H_
class Person
{
public:
	Person(const char* s);//构造函数
	~Person();//析构函数
	void print();//函数原型,没有实际body,有一些什么样的函数
	//...
//private:
	char* name ;//数据成员 char* instead of string
	//...
};
#endif // !PERSON_H_
 
//person.h
#include "person.h"
#include
#include
using namespace std;
 
//在.cpp文件中定义在.h文件中声明的那些东西的实体
Person::Person(const char* s)
{
	name = new char[::strlen(s)+1];
	::strcpy(name,s);
}
Person::~Person()
{
	//delete[] name;
}
 
void Person::print()
{
	cout << "something" << endl;
}
//person.cpp
#include
#include "person.h"
using namespace std;
 
int main()
{
	Person p1("Jhon");
	Person p2 = p1; //Person p2(p1);
	printf("p1.name=%p\n", p1.name);  // p1 name 的地址
	printf("p2.name=%p\n", p2.name);
	return 0;
}
//main.cpp

 

 地址一样,报错原因:指向同一块内存,被析构了两次。

p1 和 p2 指向同一块内存,即 Copy pointer,而我们想要 Copy entire block。 

 character strings

C的字符串:

 standard c library string functions

#ifndef PERSON_H_ #define PERSON_H_ class Person { public: Person(const char* s);//构造函数 Person(const Person& w);//拷贝构造函数 ~Person();//析构函数 void print();//函数原型,没有实际body,有一些什么样的函数 //... //private: char* name ;//数据成员 char* instead of string //... }; #endif // !PERSON_H_ //person.h

Person(const Person& w);//copy ctor
#include "person.h"
#include
#include
using namespace std;
 
//在.cpp文件中定义在.h文件中声明的那些东西的实体
Person::Person(const char* s)
{
	name = new char[::strlen(s)+1];
	::strcpy(name,s);
}
Person::Person(const Person& w)
{
	name = new char[::strlen(w.name) + 1];
	::strcpy(name, w.name);
}
Person::~Person()
{
	//delete[] name;
}
 
void Person::print()
{
	cout << "something" << endl;
}
//person.cpp
#include
#include "person.h"
using namespace std;
 
int main()
{
	Person p1("Jhon");
	Person p2 = p1;//Person p2(p1);
	printf("p1.name=%p\n", p1.name);
	printf("p2.name=%p\n", p2.name);
	return 0;
}
//main.cpp

private 是针对类而不是对象。所以访问一个同类对象的private 东西是可以的。

Person(char*) implementation
#inlcude 
ussing namespace std;

Person::Person(const char *s){
    name = new char[::strenlen(s)+1];
    ::strcpy(name,s);
}

Person::~Person(){
    delete [] name;//array delete
}
Person copy constructor

使用类的一个对象来构造另一个该类的新的对象。

when are copy constructor called?

什么时候发生拷贝构造:初始化的时候。 

什么时候调用拷贝构造:

1. 调用函数时候,函数参数是一个对象本身,而不是引用或者指针的时候,

void roster(Person);//declare function
Person child("Ruby");//create object
roster(child);//call function
。函数参数是一个对象本身意味着调用该函数时候,调用时候重新创建一个新的Person类的对象player,就会发生拷贝构造,因为使用某个对象初始化这个player。

 习惯:接受()初始化。

copies and overhead example
Person copy_func(char *who){
    Person local(who);
    local.print();
    return Peron(who);//copy ctor called
}

Person nocopy_func(char *who){
    return 
  • every object is constructed once
  • ; //no copy needed 优化掉 }
    construction vs. assignment Person:string name

    string 类的变量不用写 拷贝构造,内部实现好了。所以C++尽量用string 放弃 char*。使用 char* 只有 直接访问内存,做的是二进制数据。给人读的字符串就应该使用 string。

    copy ctor guidelines

    default constructor  

    virtual constructor  

    copy constructor

    [+++]

    拷贝构造是私有的,别人就不能拷贝构造你的对象了,即别人设计一个函数,函数的参数不能是你的类的对象的本身,* 和 & 还是可以的。 

    不建议拷贝构造是私有的。JAVA没有拷贝构造,所有对象的传递都是通过指针。

    )
    File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
    File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
    File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
    C++翁恺学习24-拷贝构造2_C_内存溢出

    C++翁恺学习24-拷贝构造2

    C++翁恺学习24-拷贝构造2,第1张

    如果类包含指针?

    Copy pointer:

    #ifndef PERSON_H_
    #define PERSON_H_
    class Person
    {
    public:
    	Person(const char* s);//构造函数
    	~Person();//析构函数
    	void print();//函数原型,没有实际body,有一些什么样的函数
    	//...
    //private:
    	char* name ;//数据成员 char* instead of string
    	//...
    };
    #endif // !PERSON_H_
     
    //person.h
    #include "person.h"
    #include
    #include
    using namespace std;
     
    //在.cpp文件中定义在.h文件中声明的那些东西的实体
    Person::Person(const char* s)
    {
    	name = new char[::strlen(s)+1];
    	::strcpy(name,s);
    }
    Person::~Person()
    {
    	//delete[] name;
    }
     
    void Person::print()
    {
    	cout << "something" << endl;
    }
    //person.cpp
    #include
    #include "person.h"
    using namespace std;
     
    int main()
    {
    	Person p1("Jhon");
    	Person p2 = p1; //Person p2(p1);
    	printf("p1.name=%p\n", p1.name);  // p1 name 的地址
    	printf("p2.name=%p\n", p2.name);
    	return 0;
    }
    //main.cpp

     

     地址一样,报错原因:指向同一块内存,被析构了两次。

    p1 和 p2 指向同一块内存,即 Copy pointer,而我们想要 Copy entire block。 

     character strings
    • in c++,a character string is
      • an array of characters
      • with a special terminator--"four" or ASCII null
    • the string "c++" is represented,in memory,by an array of size_t (4,count'em) characters

    C的字符串:

     standard c library string functions
    • declared in

      const strlen(
    • s is a null-terminated string
    • char *s);
      • returns the length of s
      • length does not include the terminator!
      • char *strcpy (char *dest,const char *src);
    • copies src to dest stopping after the terminating null-character is copied
      • dest should have enough memory space allocated to contain src string
      •  Copy entire block :

    #ifndef PERSON_H_ #define PERSON_H_ class Person { public: Person(const char* s);//构造函数 Person(const Person& w);//拷贝构造函数 ~Person();//析构函数 void print();//函数原型,没有实际body,有一些什么样的函数 //... //private: char* name ;//数据成员 char* instead of string //... }; #endif // !PERSON_H_ //person.h

    Person(const Person& w);//copy ctor
    #include "person.h"
    #include
    #include
    using namespace std;
     
    //在.cpp文件中定义在.h文件中声明的那些东西的实体
    Person::Person(const char* s)
    {
    	name = new char[::strlen(s)+1];
    	::strcpy(name,s);
    }
    Person::Person(const Person& w)
    {
    	name = new char[::strlen(w.name) + 1];
    	::strcpy(name, w.name);
    }
    Person::~Person()
    {
    	//delete[] name;
    }
     
    void Person::print()
    {
    	cout << "something" << endl;
    }
    //person.cpp
    #include
    #include "person.h"
    using namespace std;
     
    int main()
    {
    	Person p1("Jhon");
    	Person p2 = p1;//Person p2(p1);
    	printf("p1.name=%p\n", p1.name);
    	printf("p2.name=%p\n", p2.name);
    	return 0;
    }
    //main.cpp

    private 是针对类而不是对象。所以访问一个同类对象的private 东西是可以的。

    Person(char*) implementation
    #inlcude 
    ussing namespace std;
    
    Person::Person(const char *s){
        name = new char[::strenlen(s)+1];
        ::strcpy(name,s);
    }
    
    Person::~Person(){
        delete [] name;//array delete
    }
    Person copy constructor
    • to Person declaration add copy constructor prototype

      Person::Person char(const Person& w){
          name = char[::strlen(w.name)+1];
          ::strcpy(name,w.name);
      }
    • to Person .cpp add copy ctor definition

      隐藏调用
    • no value return

    • accesses "w.name" across client boundary

    • the copy ctor initializes uninitialized memory

    使用类的一个对象来构造另一个该类的新的对象。

    when are copy constructor called?

    什么时候发生拷贝构造:初始化的时候。 

    什么时候调用拷贝构造:

    1. 调用函数时候,函数参数是一个对象本身,而不是引用或者指针的时候,

    void roster(Person);//declare function
    Person child("Ruby");//create object
    roster(child);//call function
    。函数参数是一个对象本身意味着调用该函数时候,调用时候重新创建一个新的Person类的对象player,就会发生拷贝构造,因为使用某个对象初始化这个player。

    • during call by value  隐藏调用

      Person baby_a("Fred");
      //these use the copy ctor
      Person baby_b = baby_a;//not an assignment
      Person baby_c(baby_a);//not an assignment

    • during initialization 2.初始化时。 直接调用

      Person captain(){
          Person player("George");
          return player;
      }
      ...
      Person who("")
      ...

     习惯:接受()初始化。

    • during function return    3.函数返回一个对象时。

    • compilers can "optimize out" copies when safe  把拷贝构造优化掉

    copies and overhead
    • program for "dumb" compilers
    • programmers need to
      • be ready to look for optimizations
      • local
    example
    Person copy_func(char *who){
        Person local(who);
        local.print();
        return Peron(who);//copy ctor called
    }
    
    Person nocopy_func(char *who){
        return 
  • every object is constructed once
  • ; //no copy needed 优化掉 }
    construction vs. assignment
    • failure to invoke delete()
    • every object should be destroyed once
      • invoking delete() more than once
      • once an object is constructed,it can be the target of many assignment operations
    • const string&
    Person:string name

    string 类的变量不用写 拷贝构造,内部实现好了。所以C++尽量用string 放弃 char*。使用 char* 只有 直接访问内存,做的是二进制数据。给人读的字符串就应该使用 string。

    • what if the name was a string (and not a char*)

      #include 
      class Person{
      public:
          Person(string name);
          ~Person();
          void print();
          //...other accessor fxns
      private:
          don't rely on the default;//embedded object(composition)
          //...other data members
      };
    • in the default ctor ,the compiler recursively calls the copy ctors for all member objects(and base classes)

    • default is memberwise initialization

    copy ctor guidelines
    • in general,be explicit
      • create your own copy ctor--
      • prevents creation of a a default copy constructor
    • if you don't need on declare a private copy ctor
      • generates a compiler error if try to pass-by -value
      • don't need a definition
      • 一旦写一个类就写下:

    default constructor  

    virtual constructor  

    copy constructor

    拷贝构造是私有的,别人就不能拷贝构造你的对象了,即别人设计一个函数,函数的参数不能是你的类的对象的本身,* 和 & 还是可以的。 

    不建议拷贝构造是私有的。JAVA没有拷贝构造,所有对象的传递都是通过指针。

    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://outofmemory.cn/langs/713679.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-04-24
    下一篇 2022-04-24

    发表评论

    登录后才能评论

    评论列表(0条)

    保存