在C ++中使用类 [英] using classes in C++
问题描述
在c#中,我将类用作
class1 c = new class1("contractor val");
在C ++中我怎么看?
thnx寻求帮助
in c# im use class as
class1 c=new class1("contractor val");
in c++ how i that ?
thnx for any help
推荐答案
在C#中,所有类都使用引用语义.类实例始终使用堆来存储数据.值类型-结构,原始类型和枚举-始终使用值语义.它们使用标记为静态"的静态内存或声明为局部变量的堆栈内存.
相反,C ++类使您可以选择值或引用语义.
为了参考语义和使用堆,可以像在示例中一样使用关键字new
声明变量.
另外,您可以创建一个这样的实例:
In C#, all classes use reference semantics; and class instances always use heap to store data. Value type -- structures, primitive types and enums -- always use value semantic. They use either static memory is marked "static" or stack memory if declared as local variables.
In contrast, C++ classes give you a choice of value or reference semantic.
For reference semantic and use of heap, you declare variables like you do in your example using keywordnew
.
Alternatively, you create an instance like this:
class1 c("my string");
在这种情况下,将使用值语义.
使用C ++,您还可以将自己的内存分配方法与类一起使用.为此,您可以提供自定义的运算符new
和delete
:
In this case, value semantics is used.
With C++ you can also use your own memory allocation methods with classes. To do this, you can provide customized operators new
and delete
:
class Myclass
{
public:
void* operator new(size_t);
void operator delete(void*);
};
有关更多详细信息,请参见:
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C ++ Programming-HOWTO-9.html [ http://www.cprogramming.com/tutorial/operator_new.html [
For futher detail please see:
http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-9.html[^]
http://www.cprogramming.com/tutorial/operator_new.html[^]
or google for similar manuals -- there are a lot.
Thank you.
Happy New Year!
这篇关于在C ++中使用类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!