[英]c++ using a different class for an argument?
I have a simple question, when I'm writing the .h file of a class and want to pass an argument of type of a different class how should it be written? 我有一个简单的问题,当我编写一个类的.h文件并想要传递不同类的类型的参数时,应如何编写?
For example: 例如:
#include "y.h"
class x
{
public :
void method( y &)
};
In void method
, is that right? 用
void method
,对吗? Or should it be written as y::y&
? 还是应该将其写为
y::y&
? And when it's implemented in a .cpp file? 以及何时在.cpp文件中实现?
If your class is called y
then what you have written is correct. 如果您的班级叫
y
那么您所写的内容是正确的。 The ::
syntax is for referencing names within namespaces or other classes. ::
语法用于引用名称空间或其他类中的名称。 In this particular case, y::y
would refer to the constructor of y
, not the class itself. 在这种特殊情况下,
y::y
将提及的构造y
,而不是类本身。
There is no change to this within the implementation (.cpp) file - the name y
references the same class in both cases. 实施(.cpp)文件中对此没有任何更改-名称
y
在两种情况下都引用相同的类。
If do not take into account qualifier volatile then you have the following possibilies 如果不考虑限定词的易变性,那么您有以下可能性
void method( y &);
void method( const y &);
void method( y );
void method( const y * );
void method( y * );
Declaration 宣言
void method( const y );
declares the same function as 声明与
void method( y );
Also the method itself can have qualifier const. 方法本身也可以具有限定符const。 For example
例如
void method( y &) const;
Also if the class name will be hidden then you can use the elaborated name. 同样,如果将隐藏类名,则可以使用详细的名称。 For example
例如
void method( class y &) const;
This declaration 这个宣言
void method( y::y &);
is correct provided that the left y is the name of a namespace and the right y is the name of a class defined in the namespace. 是正确的,前提是左侧y是名称空间的名称,右侧y是名称空间中定义的类的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.