Thread: Struct and class

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Struct and class

    Is it conventional to use both structs and classes in c++?i recall abachler saying he used struct for objects without member functions,i like this idea as providing clarity while working, to distinguish a complex data type from an object that 'does' things but is this valid c++?
    Last edited by rogster001; 01-25-2010 at 04:17 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yes, it is often a convention to use both, but for slightly different purposes. As for valid C++: duh. You can write object oriented C++ and have fun with class templates without ever using the class keyword.
    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

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think it is but some disagree with me. My personal policy is this:

    1. A struct cannot have methods, but may have a constructor in order to initialize data. A struct should not encapsulate any functionality.
    2. A class must encapsulate some type of functionality. If a class merely wraps access to variables - IE: is only composed of accessors and mutators - then it should most likely be a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. returning class and struct members
    By simone.marras in forum C++ Programming
    Replies: 17
    Last Post: 03-16-2009, 11:10 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM