[解决]如何在C ++中将堆栈声明转换为堆声明 [英] [Solved] How to convert stack declarations to heap declarations in C++

查看:76
本文介绍了[解决]如何在C ++中将堆栈声明转换为堆声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助将以下堆栈声明转换为堆声明

Can anyone plz help to convert the following stack declarations to heap declarations

class student
{
   protected:
   int r;
   char sta,m;
   char student_roll[15];
   char name[50];
   char sex[10];
   char status[50];
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   char dob[20];
   public:
   char roll_no[15];
};





请帮助....它是一个C ++类...



Please help....its a C++ class...

推荐答案

为什么?



这是一个可接受的安全类声明;你不想保留不安全的指针只是为了它的乐趣。



一种更好的方法是用std :: string替换char数组。



用enum替换性别字符串(并且有代码打印代表而不是保留字符串(男/女)。



使用正确的描述性变量名称。



Why?

This is an acceptable and "safe" class declaration; you do not want to keep unsafe pointers just for the fun of it.

One way to make it better would be to replace the char array with std::string.

Replace the sex string with an enum (and have code to "print" the representation instead of keeping a string (male/female).

Use proper descriptive variable names.

class student
{
  enum SEX {
    Female = 0,
    Male
  };
 
   protected:
   int r;
   char sta,m;
   std::string student_roll;
   std::string name;
   SEX sex;
   std::string status;
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   std::string dob;
   public:
   std::string roll_no;
};


你正确回答了nv3!

分配内存取决于你如何创建类的对象

例如:

You correctly answered nv3!
Where memory is allocated depends on how you create an object of your class
For example:
{
student s; // allocated on stack 
}






but

student* pStud = new student; // allocated on heap



但也许你想知道如何描述一个总是只能在堆上分配的类......请使用命名构造函数成语。

见这里: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Constructor



这是你的类的新版本(如果你想在堆上只为> 对象分配一个内存):


But perhaps you're wondering how to describe a class that can always be allocated ONLY on heap... Please use the "Named Constructor Idiom".
See here : http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Constructor

Here new version of your class (if you want allocate a memory for object of class only on heap):

class student
{
public:
    // The create() methods are the "named constructors":
   static student* create() { return new student(); }

protected:
   int r;
   char sta,m;
   char student_roll[15];
   char name[50];
   char sex[10];
   char status[50];
   float slc,p2,m1,m2,m3,m4,m5,m6,m7,m8;
   float tp,p1,rem,pa;
   char dob[20];
   public:
   char roll_no[15];

private:
   // The constructor IS private !!!!
  student();
};




 //Now exist only one way to create a student objects => a student::create() 
int main()
{
   student* p = student::create();
   ...
   delete p;
   ...
} 


我建​​议你读一下堆栈上的存储与堆上的存储有何不同(在它是如何分配以及如何被处理的术语 - 它是否具有固定地址,或者它是否可能相对于某个其他值的地址......可能是CPU中寄存器中的值)。



您还应该研究C ++程序中的哪种声明会导致一种或另一种存储,您应该了解类型(类)之间的区别您在程序中声明的那些类型的实例(这是正在运行的程序中的变量,实际占用内存)。
I recommend that you read up about how storage on the stack differs from storage on the heap (in terms of how it is allocated and how it is addressed - does it have a fixed address, or is its likely address relative to some other value ... perhaps a value in a register in the CPU).

You should also study what kind of declarations in your C++ program lead to one or the other kind of storage, and you should learn about the difference between types (classes) that you declare in your program and instances of those types (that are variables in a running program, that actually occupy memory).


这篇关于[解决]如何在C ++中将堆栈声明转换为堆声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆