Jump to content

C++ Global Function/ Class Function

Featured Replies

So I'm studying for my final and I'm trying to understand what are some of the tradeoffs between creating a class function or a global function (consider operator functions I suppose, my professor mentioned that some operator functions MUST be global, and I didn't really understand). If anyone could help me understand this I would be grateful, thanks!

Suppose you're writing classes to represent vectors as defined mathematicians. (Note: *not* the ugly things in the C++ standard library.)

 

For example, the product of a vector and a scalar is a vector. That's easy: Just overload operator* in your Vector class. However, the product of a scalar and a vector also is a vector. Now you've got a problem. The argument to operator* as a member function is a multiplier, not a multiplicand. You have but no choice but to make this overload a friend function rather than a member function.

  • 3 weeks later...


I am trying to understand how global variables and functions work in C.

 

void do_work()

{

int bar = foo();

}

 

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.