简体   繁体   English

成员函数参数可以是相同的类类型吗?

[英]Can member function arguments be of the same class type?

//node.h 
class node 
{
      public:
            void sort(node n);

};

I didn't try the code yet . 我还没有尝试过代码。 But It's interesting to know if is this a valid case and Why ? 但有趣的是,这是一个有效的案例,为什么?

Edit : 编辑:

This leads me to another question : Can I declare FOO inside a member function like this ? 这引出了另一个问题:我可以在这样的成员函数中声明FOO吗?

//FOO.h
Class FOO
{
   public:
   void sort(int n) ;
   void swap(int x , int y ); 
}

//FOO.cpp

void FOO::sort (int n)
{
     FOO obj;
     obj.swap(3 , 5) ;
}

Yes, that's perfectly valid. 是的,这完全有效。 If you couldn't do that, how could you write copy constructors? 如果你不能这样做,你怎么能写复制构造函数?

A similar, but not valid case, is including the same type as a member of your class/struct. 类似但无效的情况包括与类/结构的成员相同的类型。

struct Foo
{
  Foo m_foo;
};

You can't do that, because it's essentially a circular definition, and if you had: 你不能这样做,因为它基本上是一个循环定义,如果你有:

struct Foo
{
  Foo m_foo;
  Foo m_bar;
};

Then a Foo would be 2 Foos, which would be 4 Foos, which would be 8 Foos etc. which makes no sense. 然后一个Foo将是2个Foos,这将是4个Foos,这将是8个Foos等,这没有任何意义。

You can, on the other hand, have pointers to the same type: 另一方面,您可以指向相同类型的指针:

struct Foo
{
  Foo* m_foo;
};

Because a pointer to Foo isn't the same as Foo. 因为指向Foo的指针与Foo不同。 A pointer is a pointer, no matter what it points to, so there is no circular definition or dependencies. 无论指向什么指针,指针都是指针,因此没有循环定义或依赖关系。

Having now established that your question is solely related to passing node by value in a member (rather than passing node* or node& ) the answer is still yes. 现在已经确定您的问题仅与在成员中按值传递node (而不是传递node*node& )有关,答案仍然是肯定的。 You can even define the body of the member within the class is you so wish. 你甚至可以在课堂上定义成员的身体。

As to why, think of things from the compiler's point of view. 至于为什么,从编译器的角度思考问题。 As it parses the class all it really needs to know are what data members there are within it and what the function member signatures are. 因为它解析了类所有它真正需要知道的是它内部的数据成员以及函数成员签名是什么。 None of the function member definitions need to be parsed at this point so the compiler would not be alarmed at the prospect of seeing an actual node as an argument. 此时不需要解析任何函数成员定义,因此编译器不会因将实际node视为参数而受到警告。 Only when it's finished parsing the data and signatures would it go back and actually deal with the function member definitions and at that point it knows precisely what a node is. 只有当它完成解析数据和签名时,它才会返回并实际处理函数成员定义,并且在那时它确切地知道node是什么。

In answer to your second question you can define instances of your class within the member (for the same reason). 在回答第二个问题时,您可以在成员中定义类的实例(出于同样的原因)。

yes, you can. 是的你可以。 its a valid case. 这是一个有效的案例。

you can find some sample in c++ standard libs : 你可以在c ++标准库中找到一些样本:

string& append( const string& str );

This is done quite often. 这是经常做的。 A copy constructor is aa good example: 复制构造函数是一个很好的例子:

class Apple {  
public:  
    Apple();  
    Apple(const Apple&);  
};  

The short answer is Yes: 简短的回答是:

The long answer is: 答案很长的答案是:

$9.2/2- "A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments and constructor ctor-initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification." $ 9.2 / 2-“类在类别说明符的结束时被认为是一个完全定义的对象类型(3.9)(或完整类型)。在类成员规范中,该类在函数体内被认为是完整的,默认参数和构造函数ctor-initializers(包括嵌套类中的这类东西)。否则它在自己的类成员规范中被认为是不完整的。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类的静态成员是否可以与C ++中的成员属于同一类型 - Can a static member of a class as the same type as the class it is member of in C++ 为什么一个类成员函数不能接受与其类相同类型的多个参数 - Why can't a class member function take more than one argument of the same type as its class 具有参数的类的pthread成员函数 - pthread member function of a class with arguments 类的成员是否可以与其类型(另一个类)命名相同的名称? - Can a member of a class be named the same name as its type (another class)? 如何使类成员变量与函数模板的返回类型相同? - How to make a class member variable be the same type of return of a function template? 推导成员函数参数/返回类型 - Deduce member function arguments / return type 成员变量和函数参数的模板类型推导 - Template type deduction for member variables and function arguments 使用可变参数调用模板类型作为成员函数 - Calling templated type with variadic arguments as a member function 类的成员函数,带有模板参数和类外部的默认参数 - Member function of class with template arguments and default arguments outside class 为什么类不能为函数和数据成员具有相同的名称? - Why can't a class have same name for a function and a data member?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM