structs and classes

Are structs as powerful as classes in c++? Or could it be that they use less memory but with removed functions?
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
also structures are public by default and classes are private by default
so structs are just classes minus being "public by default".
No no, classes are just structs plus being private by default :p
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!
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.
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
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
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.