为什么在 C++ 类定义中使用公共引用 [英] Why public ref in c++ class definition

查看:53
本文介绍了为什么在 C++ 类定义中使用公共引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说明的是,我对 C++ 完全陌生,所以这可能是一个简单且有些明显的问题.在我正在阅读的 C++ 书籍 C++ Primer 中,通过以下方式定义了一个类:

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:

};

然而,在 VS2008 中,编译器并不喜欢这样.但是通过在类之前添加公共引用,如:

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.

推荐答案

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++ 类定义中使用公共引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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