SEPS
SEPS

Reputation: 105

c++ : how can I call a method in main function without creating an object

I have the following class:

class Vector
{
public:
    float x_;
    float y_;
    float z_;

    Vector(float x, float y, float z) : x_(x), y_(y), z_(z) {}
    float scalar(Vector a, Vector b);
};

and the following method:

float Vector::scalar(Vector a, Vector b)
{
  return a.x_ * b.x_ + a.y_ * b.y_ + a.z_ * b.z_;
}

now I am initializing Vector a and Vector b in main function:

int main()
{
  Vector a = Vector(1,2,3);
  Vector b = Vector(4,5,6);

  float result;
  // how can I call the scalar() method and save the return value in result

  return 0;
}

But now I am wondering how to call the scalar() method. I've tried to declare scalar() static but it did not work.

Upvotes: 0

Views: 228

Answers (6)

2785528
2785528

Reputation: 5566

My first answer provided 2 choices that avoided changing any of the class code you have. Rationale: An activity that most professional programmers get involved with (ahem, 'assigned'), is a) adding functionality, and b) responding to a change in requirements. And of course, there is c) the 'fix' to coding errors. Your efforts will be smaller (and thus sooner complete, and thus better received) if you minimize impact to existing code.

This answer suggests adding 2 lines to your existing class Vector,

  1. add the default ctor (Note - the default ctor and dtor do nothing)
  2. add operator()

These two 1 liner additions allow you to use the class as a functor.

From Google, C++ functor:

A functor (or function object) is a C++ class that acts like a function. Functors are called using the same old function call syntax. To create a functor, we create a object that overloads the operator().

Functors in C++ - GeeksforGeeks https://www.geeksforgeeks.org/functors-in-cpp/


class Vector
{
public:

   // ... as above


   // functor style access:
   Vector() = default;                   // default ctor
   float operator()(Vector a, Vector b) { return scalar(a,b); } // operator()

}; // class Vector

how to call the scalar() method?

Through the functor:

int main()
{
  Vector a = Vector(1,2,3);
  Vector b = Vector(4,5,6);

  float result = Vector()(a,b);

  std::cout << "\n\n  " << result 
            <<   "      even more convenient: " << Vector()(a, b) << std::endl;

  return 0;
}

Upvotes: 0

2785528
2785528

Reputation: 5566

You are trivially close ...

Since the ctor and dtor are 'trivial', you can simply instantiate a dummy Vector, and use that dummy's scalar method.

But if you can't bring yourself to trivialize the cost (of the ctor / dtor) or you know you will be incrementally growing their behaviour, you can also make use of either of the already existing Vector's. Note that the method Vector::scalar() does nothing to the local data. Thus, no 'static' required. No code changes required.

int main()
{
  Vector a = Vector(1,2,3);
  Vector b = Vector(4,5,6);

  // instantiate a dumy vector
  float result1 = Vector(0, 0, 0).scalar(a,b);  // dummy Vector

  // use an existing vector
  float result2 = a.scalar(a,b);  // method scalar does not modify self.

  std::cout << "\n\n  " << result1 << "  " << result2 << std::endl;

  return 0;
}

Both results are same, i.e. 32

Upvotes: 0

user2827968
user2827968

Reputation:

How about?

float Vector::scalar(const Vector& other)
{
  return x_ * other.x_ + y_ * other.y_ + z_ * other.z_;
}

int main()
{
  Vector a(1,2,3);
  Vector b(4,5,6);

  float result = a.scalar(b);

  return 0;
}

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92221

Unlike some other languages, C++ lets you have functions that are not members of a class. You can just define the function outside the Vector, and then call it without an object prefix.

class Vector
{
public:
    float x_;
    float y_;
    float z_;

    Vector(float x, float y, float z) : x_(x), y_(y), z_(z) {}
};

float scalar(Vector a, Vector b)
{
  return a.x_ * b.x_ + a.y_ * b.y_ + a.z_ * b.z_;
}

int main()
{
  Vector a(1,2,3);
  Vector b(4,5,6);

  float result = scalar(a, b);

  return 0;
}

Upvotes: 4

Daniel Duvilanski
Daniel Duvilanski

Reputation: 141

You need to define scalar as static and call it as a static method:

class Vector
{
    ...

    static float scalar(Vector a, Vector b);
};

and

auto result = Vector::scalar(a,b);

Upvotes: 2

Ra&#39;Jiska
Ra&#39;Jiska

Reputation: 1049

What you can do is create your method as a static method. It would have the same effect as namespacing your method with the class name.

class Vector
{
public:
    float x_;
    float y_;
    float z_;

    Vector(float x, float y, float z) : x_(x), y_(y), z_(z) {}
    static float scalar(Vector a, Vector b);
};

Which you can call the following way in your main:

int main()
{
  Vector a = Vector(1,2,3);
  Vector b = Vector(4,5,6);

  float result = Vector::scalar(a, b);
  return 0;
}

Upvotes: 3

Related Questions