简体   繁体   English

不带::的.cpp中的C ++类方法调用

[英]C++ class method call in .cpp without ::

I'm new to progamming in C++. 我是C ++编程新手。 I have a good background with Java but C++ is different on many things and I have a question about one of the regarding .h and .cpp files. 我有Java的良好背景,但是C ++在许多事情上是不同的,并且我对有关.h和.cpp文件之一有疑问。

I have the following files for a point object with x and y position: 对于具有x和y位置的点对象,我具有以下文件:

Point.h

#ifndef POINT_H_
#define POINT_H_

class Point{
Point();
Point(int newX, int newY);

public:
    int getX();
    int getY();
    void setX(int newX);
    void setY(int newY);
    void moveBy(int moveX, int moveY);
    Point reverse();
private:
    int x;
    int y;
};
#endif

Point.cpp 点.cpp

#include "Point.h"

using namespace Point;

Point::Point(int newX, int newY){
    x = newX;
    y = newY;
}
int Point::getX(){
    return x;
}
int Point::getY(){
    return y;
}
void Point::setX(int newX){
    x = newX;
}
void Point::setY(int newY){
    y = newY;
}
void Point::moveBy(int moveX, int moveY){
    x += moveX;
    y += moveY;
}
Point Point::reverse(){
    return Point(y,x);
}

I was wondering if there was a way of avoinding the Point::Point part like with std::cout by using namespace. 我想知道是否有一种通过使用命名空间来避免与std :: cout一样的Point::Point部分的方法。

Thank you 谢谢

You aren't required to separate your declaration and definition, and these functions are incredibly trivial. 不需要将声明和定义分开,并且这些函数非常琐碎。 So including them in the class definition may actually allow the compiler to perform numerous additional optimizations. 因此,将它们包括在类定义中实际上可能允许编译器执行许多其他优化。

So you could discard the .cpp entirely and the header becomes: 因此,您可以完全丢弃.cpp,并且标头变为:

#ifndef POINT_H_
#define POINT_H_

class Point
{
    int x_ { 0 };
    int y_ { 0 };

public:
    Point() = default;
    Point(int x, int y) : x_(x), y_(y) {}

    int getX() const { return x_; }
    int getY() const { return y_; }
    void setX(int x) { x_ = x; }
    void setY(int y) { y_ = y; }
    void moveBy(int x, int y) { x_ += x, y_ += y; }
    Point reverse() const { return Point(y_, x_); }
};

#endif

But you can't avoid the "Point::" part when defining the members outside of the class declaration. 但是在类声明之外定义成员时,您无法避免使用“ Point ::”部分。

If you want to avoid typing the " Point:: " in front of the getX , getY etc., then the answer is "no" , unfortunately. 如果要避免在getXgetY等前面键入“ Point:: ”,那么不幸的是答案是“ no” In C++ and the name of any class (like " Point ") is not a namespace , it's a scope . 在C ++中,任何类的名称(例如“ Point ”)都不是名称空间 ,而是作用域

What you can only do is inlining the method, defining into the class declaration. 您只能做的是将方法内联 ,定义到类声明中。

class Point {
  public:
    void a_method() {
      // all the code here!
    }
};

You can't avoid "the Point::Point part," unless you declare the construction inline in the class declaration. 您不能避免使用“ Point::Point部分”,除非您在类声明中声明了内联构造。 The first "Point" defines the scope of the function, and the second "Point" is the name of the constructor function. 第一个“ Point”定义函数的范围,第二个“ Point”是构造函数的名称。

However, you could define the constructor(s) inline, like so: 但是,您可以内联定义构造函数,如下所示:

class Point
{
  Point()
  {
    x = 0;
    y = 0;
  }
  Point(int newX, int newY);
  {
    x = newX;
    y = newY;
  }
  // ...
};

Or: 要么:

class Point
{
  Point() : x(0), y(0) {}
  Point(int newX, int newY) : x(newX), y(newY) {}
  // ...
};

Or: 要么:

class Point
{
  Point(int newX = 0, int newY = 0) : x(newX), y(newY) {}
  // ...
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM