What does “ClassName ClassName::FunctionName” mean

2019-06-09 23:12发布

When I read c++ code, I keep seeing code of the form "ClassName ClassName::FunctionName." For example:

Fraction Fraction:: Reciprocal() {       

    return Fraction(denominator,numerator);
}

In above Fraction is a class name and Reciprocal() is a function declared inside fraction class and there is a constructor with two parameters. What is the significant of using this format?

5条回答
霸刀☆藐视天下
2楼-- · 2019-06-09 23:57

You should read up on how methods and functions work. The above piece of code is a method of the class Fraction which returns a Fraction object.

The significance of this method is that instead of modifying itself, it can instead modify a new Fraction and output that, leaving itself exactly the same.

Using methods like these are only really useful in a mathematical context, like with Matrices or Trees, and it is so you can perform mathematical operations on certain objects without actually modifying the object.

查看更多
我命由我不由天
3楼-- · 2019-06-10 00:01

so that means that the First Fraction indicates the return type just like void, double, int etc.... you are returning Fraction, and the second Fraction is the let the compiler know the function Reciprocal() is the one from the Fraction class, that's it, don't be trussed by the way it looks, it is simple as that, if you want to read more up on it try to get the "absolute c++" it is a good book.

查看更多
Ridiculous、
4楼-- · 2019-06-10 00:02

Since you are returning a Fraction object:

Fraction Fraction::Reciprocal() {       

    return Fraction(denominator,numerator);
} //       ^^^^^^^^

The return type is:

  Fraction Fraction::Reciprocal() {       
//^^^^^^^^

Since Reciprocal() method is inside Fraction class you are writing the class that the method belongs to followed by the :: operator to access it:

  Fraction::Reciprocal()
//^^^^^^^^^^
查看更多
做自己的国王
5楼-- · 2019-06-10 00:09

There's nothing significant or special about this combination. It has no special meaning as a whole, as you seem to believe. It is an ordinary everyday run-of-the-mill member function definition, not different in any significant way from any other C++ member function definition.

Its meaning follows from the basic rules of C++ language. This

Fraction Fraction::Reciprocal() {  
  ...

Is a definition of method Reciprocal from class Fraction. The return type of this method is Fraction. So, a method of class Fraction returns an instance of the same class Fraction - there's nothing weird, unusual or special in that.

The rest of the definition simply constructs that instance as Fraction(denominator,numerator) and returns it. Again, this is a ordinary everyday way to create a temporary instance of class Fraction.

So, to answer your question: there's nothing "significant" about this "format". There's no special "format" here of any kind, just a bunch of simple C++ features, completely unrelated to each other. Everything is very plain and basic. Any basic C++ book will have an explanation of these features.

P.S. One can note that a method like this is a good candidate for being declared const

Fraction Fraction::Reciprocal() const {  
  ...

but it is a separate issue.

查看更多
▲ chillily
6楼-- · 2019-06-10 00:10

In C++, if you implement a member function outside of the class definition, then you need to indicate (in addition to the return type and the argument types) that the function you're writing is indeed a member function. If you instead wrote

Fraction Reciprocal() {
    // ...
}

then C++ would interpret this as if you'd written a free function (not a member function) named Reciprocal. On the other hand, writing

Fraction Fraction::Reciprocal() {
    // ..
}

tells C++ that you're implementing something nested inside of Fraction.

The :: notation is called the scope resolution operator. Think of it like a / in the path to a file. If you think of Fraction as a folder and Reciprocal, the member function, as a file in that folder, then you can read the above code as

Fraction  // returns a Fraction
Fraction::Reciprocal() { // function name is Reciprocal; it's declared inside Fraction
      ...
}
查看更多
登录 后发表回答