第九章:内存模型与名称空间

本文探讨了C++中构造函数的应用与内存管理技巧,包括使用构造函数初始化成员变量、利用new运算符进行内存分配及放置新建等高级主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

9.1题:构造函数的调用

#include<iostream>
#include<cstring>
using namespace std;
class Golf
{
    string m_name;
    int m_handicap;
    public:
            Golf():m_name("Sam"),m_handicap(24){};//析构函数的赋值,m_name和m_handicap在进入大括号之前就已经初始化
            void show()
            {
                cout << m_name << ',' << m_handicap <<endl;
            }
            Golf( string name,int handicap)
            {
                m_name = name;
                m_handicap = handicap;
            }
            
};
int main()
{
    Golf one;
    Golf two("Tonny",10086);
    one.show();
    two.show();
}

直接用string就好了,前面试着用strcpy赋值很麻烦


上面其实是10.1的答案,以下才是9.1

共有golf.h, golf.cpp9-1golf.cpp 三个文件

golf.h头文件:

 1 //golf.h 
 2 const int Len = 40;
 3 struct golf
 4 {
 5  char fullname[Len];
 6  int handicap;
 7 };
 8 // non-interactive version
 9 // function sets golf structure to provided name, handicap
10 // using values passed as arguments to the function
11 void setgolf(golf & g, const char * name, int hc);
12 // interactive version
13 // function solicits name and handicap from user
14 // and sets the members of g to the values entered
15 // returns 1 if name is entered, 0 if name is empty string
16 int setgolf(golf & g);
17 // function resets handicap to new value
18 void handicap(golf & g, int hc);
19 // function displays contents of golf structure
20 void showgolf(const golf & g);

golf.cpp:

 1 //golf.cpp
 2 #include <iostream>
 3 #include "golf.h"
 4 #include <cstring>
 5 // function solicits name and handicap from user
 6 // returns 1 if name is entered, 0 if name is empty string
 7 int setgolf(golf & g)
 8 {
 9     std::cout << "Please enter golfer's full name: ";
10     std::cin.getline(g.fullname, Len);
11      if (g.fullname[0] == '\0')
12      return 0; // premature termination
13     std::cout << "Please enter handicap for " << g.fullname << ": ";
14      while (!(std::cin >> g.handicap))
15     {
16         std::cin.clear();
17         std::cout << "Please enter an integer: ";
18     }
19      while (std::cin.get() != '\n')
20      continue;
21      return 1;
22 }
23 // function sets golf structure to provided name, handicap
24 void setgolf(golf & g, const char * name, int hc)
25 {
26     std::strcpy(g.fullname, name);
27     g.handicap = hc;
28 }
29 // function resets handicap to new value
30 void handicap(golf & g, int hc)
31 {
32     g.handicap = hc;
33 }
34 // function displays contents of golf structure
35 void showgolf(const golf & g)
36 {
37     std::cout << "Golfer: " << g.fullname << "\n";
38     std::cout << "Handicap: " << g.handicap << "\n\n";
39 }

在17行后加一行代码//cin.ignore(1024,'\n');

这样输入非整数后还可以修改,不然会陷入死循环。

9-1golf.cpp:

//9-1golf.cpp
#include <iostream>
#include "golf.h"
// link with golf.cpp
const int Mems = 5;
int main(void)
{
     using namespace std;
     golf team[Mems];
    cout << "Enter up to " << Mems << " golf team members:\n";
     int i;
     for (i = 0; i < Mems; i++)
     if (setgolf(team[i]) == 0)
     break;
     for (int j = 0; j < i; j++)//如果过早终止了,就可以按现有输入的运行。 
     showgolf(team[j]);
     setgolf(team[0], "Fred Norman", 5);
     showgolf(team[0]);
     handicap(team[0], 3);
     showgolf(team[0]);
     return 0;
}

三个文件在一个项目里编译。

 9.3题:

熟悉一下new运算符

#include <iostream>
#include <new>
#include <cstring>
struct chaff
{
	char dross[20];
 	int slag;
};
	char buffer[500]; // option 1
	int main()
{
	using std::cout;
	using std::endl;
	 chaff *p1;
	 int i;
	 //char * buffer = new char [500]; // option 2
	 p1 = new (buffer) chaff[2]; // place structures in buffer
	 //在位于buffer的内存块中申请两个chaff的结构空间,官方称placement new; 
	 
	 
	 std::strcpy(p1[0].dross, "Horse Feathers");
	 p1[0].slag = 13;
 	 std::strcpy(p1[1].dross, "Piffle");
  	 p1[1].slag = -39;
 	 for (i = 0; i < 2; i++)
 	 cout << p1[i].dross << ": " << p1[i].slag << endl;
 	//delete [] buffer; // option 2
 	return 0;
} 

option1 和option2 可以互换。 

option是静态数组作用于缓冲区,option2是new操作符来分配缓冲区。 

转载于:https://www.cnblogs.com/fudianheg/p/4237512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值