question with classes

This is a discussion on question with classes within the C++ Programming forums, part of the General Programming Boards category; I have two classes: CUnit and CForce. CUnit represents a unit in the current battle (I'm making a TBS game) ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    question with classes

    I have two classes: CUnit and CForce. CUnit represents a unit in the current battle (I'm making a TBS game) and also serves as a node in the linked list, CForce. I want CForce to be a friend class of CUnit, which would require CForce to be defined b4 CUnit so the compiler would know what class I'm talking about, but CForce must be defined after CUnit because the nodes are of course, CUnit's.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: question with classes

    Originally posted by frenchfry164
    I want CForce to be a friend class of CUnit, which would require CForce to be defined b4 CUnit so the compiler would know what class I'm talking about, but CForce must be defined after CUnit because the nodes are of course, CUnit's.
    The class doesn't have to be defined prior, it just has to be declared prior -- much like a function declaration.

    Before you try and make the class a friend just write:

    class CForce; // Notice no brackets -- this is a declaration for the class, you define the class later just as you normally would.

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I read somewhere that said you can only define classes, so I didn't even think of that. Thx for clearing that up. If I ever find the guy who wrote that I'm going to strangle him.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21