structs and classes

Feb 12, 2011 at 6:53pm
Are structs as powerful as classes in c++? Or could it be that they use less memory but with removed functions?
Feb 12, 2011 at 7:06pm
closed account (zb0S216C)
They both hold constructors, destructors, methods, members, privacy, protection and inheritance. The only difference is that struct's were used in C, when classes didn't exist. However, in C struct's, privacy and protection didn't exist, member methods weren't allowed, constructors and destructors didn't exist. In C++ on the other hand, struct's are just as powerful as classes; in my opinion.
Last edited on Feb 12, 2011 at 7:07pm
Feb 12, 2011 at 7:53pm
also structures are public by default and classes are private by default
Feb 12, 2011 at 8:32pm
so structs are just classes minus being "public by default".
Feb 12, 2011 at 8:49pm
No no, classes are just structs plus being private by default :p
Feb 13, 2011 at 4:26am
closed account (D80DSL3A)
Can you derive structures from other structures and have virtual functions? Polymorphic function behavior is possible only with classes. Lot of power there!
Feb 13, 2011 at 4:26pm
sigh.

The absolutely _ONLY_ difference is what acorn/Moschops said.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Foo : /* if left unspecified, public by default */ Base
{
    /* If left unspecified, public by default */
    int member_variable;
    Foo();
    virtual void frobnicate();
    ~Foo();
    Foo& operator=( Foo rhs );
};

class Foo : /* if left unspecified, private by default */ Base
{
    /* If left unspecified, private by default */
  public:  // Better make these public...
    int member_variable;
    Foo();
    virtual void frobnicate();
    ~Foo();
    Foo& operator=( Foo rhs );
};


There is NO OTHER DIFFERENCE.
Feb 13, 2011 at 5:22pm
JSmith is completely correct. fun2code is wrong wrong wrong.

Can you derive structures from other structures and have virtual functions?


Yes.

Polymorphic function behavior is possible only with classes.


That's what we in the trade call a "big fat lie", or possibly twisting of words if you end up saying that in C++ a structure is just a class by another name.

I know JSmith made it clear, but that kind of lie needs to be really, really killed.
Last edited on Feb 13, 2011 at 5:23pm
Feb 13, 2011 at 7:56pm
closed account (D80DSL3A)
Oops - I stand corrected corrected corrected!
I should have researched it instead of going with my ill formed impression.
http://en.wikipedia.org/wiki/C%2B%2B_structures_and_classes
Feb 13, 2011 at 9:24pm
It is worth saying that a LONG LONG time ago (not even in a galaxy far far away), there WAS a big difference between class and struct in C++. But those times are gone.
Topic archived. No new replies allowed.