Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. friend function problem ...

friend function problem ...

Scheduled Pinned Locked Moved C / C++ / MFC
game-devquestionhtmldatabasecom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hadi Rezaee
    wrote on last edited by
    #1

    What is the wrong ? :confused: #include #include #include class MyMath { public: friend int Sum(int v1, int v2); }; int Sum(int v1, int v2) { return v1 + v2; } void main() { MyMath obj; cout << "Result: " << obj.Sum(1, 3); _getch(); } My month article: Game programming by DirectX by Lan Mader. Please visit in: www.geocities.com/hadi_rezaie/index.html Hadi Rezaie

    M 1 Reply Last reply
    0
    • H Hadi Rezaee

      What is the wrong ? :confused: #include #include #include class MyMath { public: friend int Sum(int v1, int v2); }; int Sum(int v1, int v2) { return v1 + v2; } void main() { MyMath obj; cout << "Result: " << obj.Sum(1, 3); _getch(); } My month article: Game programming by DirectX by Lan Mader. Please visit in: www.geocities.com/hadi_rezaie/index.html Hadi Rezaie

      M Offline
      M Offline
      moswal
      wrote on last edited by
      #2

      Your MyMath class's Sum function is public to other classes so there is not need to declare it as friend. And you can declare the method as friend if you are writing a class template(generating new classes depending upon what type of parameters you pass to the function). In your code I do not see a reason for declaring it as a friend or also it not possible to declare it as a friend. Einstein's Assistant

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups