为什么在 c++ 中公开引用 类定义
首先,我想澄清一下,我对 C++ 都是新手,所以这可能是一个简单且有些明显的问题。 在我正在阅读的 C++ 书籍《C++ Primer》中,类是通过以下方式定义的:
class classname{
public:
private:
};
但是,在 VS2008 中,编译器不喜欢这样。 但是通过在类之前添加公共引用,如下所示:
public ref class classname{
它在编译器中经过了。 谁能解释一下仅使用类定义和使用公共引用类定义之间有什么区别? 我真的很感激。
First of all I want to make clear that 'm all new to C++, so this might be a simple and somewhat obvious question. In the C++ book I'm reading called C++ Primer, a class is defined by writing:
class classname{
public:
private:
};
However, in VS2008 the compiler didnt like this. But by adding public ref before class, as in:
public ref class classname{
it went through in the compiler. Can anyone please explain what the difference is between defining only with class and with public ref class? I would really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ref
语法是仅在托管 C++ 中使用的 Microsoft 扩展。 从声音来看,您在创建项目时错误地打开了/clr
编译器开关。 如果您只想创建真正的 C++ 程序,那么您将需要恢复它。The
ref
syntax is a Microsoft extension used only in Managed C++. By the sounds of things you have flicked the/clr
compiler switch on by mistake when creating your project. If all you want to do is to create real C++ programs, then you will want to revert that.您正在阅读一本名为《C++ Primer》的书,该书教授 C++ 编程语言,而您正在尝试创建一个程序,告诉 VC++ 代码位于 C++/CLI,一种不同的编程语言......
You're reading a book call C++ Primer which teaches the C++ programming language, whereas you're attempting to create a program telling VC++ the code is in C++/CLI, a different programming language....
您(编辑:和其他用户)将此问题标记为:
但是,您的第一个目标应该是学习如何配置 Visual C++使用您选择的语言 C++ 进行编程。
像往常一样,找到此问题答案的一个好方法是在“提问”页面中输入该问题,然后查看建议的相关问题。 我认为这是最好的:编译“标准” Visual Studio 中的 C++(非 .net)。
You (edit: and other users) tagged this question with:
However, your first goal should be to learn how to configure Visual C++ to program in the language of your choice, C++.
As usual, a good way to find an answer to this question is to type it in the "Ask Question" page and see which related questions are suggested. This is the best one, I think: compiling "standard" C++ in visual studio (non .net).
您正在学习标准 C/C++ 语言吗? 如果答案是肯定的,那么该语法与 Microsoft C++/CLI 语言有关...
请查看: C++/CLI
尝试使用带有 C++ 扩展的 Eclipse。 它配备了创建和执行标准 C++ 程序所需的一切!
Eclipse 下载
Are you learning the standard C/C++ language? if the answer is yes then that syntax is concerning the Microsoft C++/CLI language...
look at : C++/CLI
Try to use Eclipse with C++ extension instead. It cames with all you need to create and execute a STANDARD C++ program!
Eclipse download