Inner classes

This is a discussion on Inner classes within the C++ Programming forums, part of the General Programming Boards category; I have a .cc file with several methods in it. I also have some structs declared at the top of ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Inner classes

    I have a .cc file with several methods in it. I also have some structs declared at the top of the file that I use inside of the .cc file. I'd like these structs to have methods, ie. I'd like the structs to be classes. Is that only a matter of replacing the word struct with class?

    Currently I have:
    Code:
    struct A
    {
         int a1;
         int a2;
    }
    I'd like to have:
    Code:
    class A
    {
         int a1;
         int a2;
         int add(void)
         {
               return a1+a2;
         }
    }
    Is that all I have to do?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can put methods inside a struct too.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I didn't know that. Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,772
    In C++, a struct is a class with base classes (i.e., inheritance) and members public by default. As Stroustrup's C++ glossary entry on struct puts it, they are "most often used for data structures without member functions or class invariants, as in C-style programming".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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