在另一个 Class C++ 的方法中创建 Class 对象

Create Class Object within Method of Another Class C++

我是 C++ 新手。如何在一个 class 中创建一个方法,用指定的参数在另一个 class 中初始化一个对象?类似于以下内容。

class A { 

public:

    double X;

    double Y;

    A(double a, double b) {

    X = a;

    Y = b;

    };

class B {

public:

 A f(double a, double b) {
    //Initialize an object of type A using parameters specified.
    };
};

我想稍后使用 A 类型的对象,所以我想我需要在 f 中使用 new 运算符。提前致谢。

试试这个:

class B {
 public:
  A* f(double a, double b) { return new A(a, b); };
};

尽管您可以轻松地 new A(a,b) 在您想做的任何地方 B.f(a,b)