|
-
March 14th, 2005, 05:45 PM
#1
Struct vs Class
So we all know the main difference between classes and structures:
Classes default as private, structures default as public.
But when it comes down to deciding whether or not to use classes or structures on a basis of performance, this small difference isn't enough.
I would like to know all known differences between classes and structures. I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes. I don't know I think this is BS, but I want to know what you guys think.
Thanks!
-
March 14th, 2005, 06:07 PM
#2
Re: Struct vs Class
 Originally Posted by MrDoomMaster
So we all know the main difference between classes and structures:
Classes default as private, structures default as public.
But when it comes down to deciding whether or not to use classes or structures on a basis of performance, this small difference isn't enough.
I would like to know all known differences between classes and structures.
You just named one -- classes have the default access specifier as private, while for struct's the default specifier is public.
One other that you didn't mention is that classes are inherited privately by default, while structs are inherited publicly.
Otherwise, there is no difference.
I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes.
Get another professor -- that's pure gobbledy-gook.
Regards,
Paul McKenzie
-
March 14th, 2005, 06:10 PM
#3
Re: Struct vs Class
I don't think that you can compare the two in any shape or form. They are two completely different things. However...
Imo, it depends on your choice. I prefer using structures from time to time (yes, I know, it's a C thang ) when I have to make dynamic information container thinga-ma-bobs (linked lists, BSTs, etc.) Classes I use in order to gain a level of abstraction from stuff that I don't want to mess up (that's a major selling point of OOP!)
-
March 14th, 2005, 06:23 PM
#4
Re: Struct vs Class
 Originally Posted by YourSurrogateGod
I don't think that you can compare the two in any shape or form. They are two completely different things.
No they are not "completely different". Except for the differences in the default access specifier, structs and classes are exactly the same thing.
Code:
struct foo
{
private:
int x;
public:
foo();
virtual int func1();
};
Is the same as
Code:
class foo
{
private:
int x;
public:
foo();
virtual int func1();
};
Regards,
Paul McKenzie
-
March 14th, 2005, 08:19 PM
#5
Re: Struct vs Class
Some reasons for using structure in C++:
1. working with a legacy program
2. communicating a structure with traditional programming API (including networking)
3. making a dumb object for database record
-
March 14th, 2005, 08:31 PM
#6
Re: Struct vs Class
 Originally Posted by Paul McKenzie
No they are not "completely different". Except for the differences in the default access specifier, structs and classes are exactly the same thing.
Code:
struct foo
{
private:
int x;
public:
foo();
virtual int func1();
};
Is the same as
Code:
class foo
{
private:
int x;
public:
foo();
virtual int func1();
};
Regards,
Paul McKenzie
Wow, never seen that done before with a struct. Now if structs can do inheritance then the world will come to an end .
-
March 14th, 2005, 09:05 PM
#7
Re: Struct vs Class
 Originally Posted by YourSurrogateGod
Now if structs can do inheritance then the world will come to an end  .
It does and the default is public inheritance. The world hasn't ended yet and life still goes on.
Last edited by Kheun; March 14th, 2005 at 09:41 PM.
-
March 14th, 2005, 10:10 PM
#8
Re: Struct vs Class
 Originally Posted by Kheun
It does and the default is public inheritance. The world hasn't ended yet and life still goes on. 
/me falls over...
-
March 15th, 2005, 12:03 AM
#9
Re: Struct vs Class
there is no need to talk about it any more,it is just household. though there may be some difference when u apply it in the your programme,it is no longer a story
regards,
jolley
-
March 15th, 2005, 01:27 AM
#10
Re: Struct vs Class
 Originally Posted by jolley
there is no need to talk about it any more,it is just household.  though there may be some difference when u apply it in the your programme,it is no longer a story
regards,
jolley
Well, to be frank, I wasn't really aware of the possibilities that struct had . I could have even simplified some of my C programs to a degree .
-
March 15th, 2005, 03:51 AM
#11
Re: Struct vs Class
 Originally Posted by MrDoomMaster
I was told by my professor that a structure directly accesses data whereas classes reference their data as a pointer kind of, making structures faster than classes. I don't know I think this is BS, but I want to know what you guys think.
Thanks!
I agree with Paul McKenzie's comments about getting another professor.
However, there are many professors with very out dated programming knowledge.
There are many teachers who teach C/C++ language who have never even seen the actual C/C++ standard.
There is a common practice among some C++ programmers in which they use struct for POD types, and class for non-POD types.
In GENERAL POD types are easy and fast to construct, and have no explicit constructors or destructors.
However, the C++ standard itself has nothing to say about this common practice, and there's nothing to stop you from using a class to create a POD type, or using a struct to create a non-POD type.
-
March 15th, 2005, 03:57 AM
#12
Re: Struct vs Class
 Originally Posted by YourSurrogateGod
Well, to be frank, I wasn't really aware of the possibilities that struct had  . I could have even simplified some of my C programs to a degree  .
Not really.
C programs do not allow struct to have member functions.
Furthermore, you can not use a constructor to create a struct object in C.
-
March 15th, 2005, 04:37 AM
#13
Re: Struct vs Class
 Originally Posted by Axter
Not really.
C programs do not allow struct to have member functions.
Furthermore, you can not use a constructor to create a struct object in C.
Well, there are not constructors and member functions in C at all.
Last edited by ovidiucucu; March 15th, 2005 at 04:40 AM.
-
March 15th, 2005, 04:54 AM
#14
Re: Struct vs Class
 Originally Posted by Paul McKenzie
Get another professor -- that's pure gobbledy-gook.
Only if that was so easy... I would have done that with several professors when I was in college...
 Originally Posted by god
me falls over...
Just sit down and take a very deep breath.
-
March 15th, 2005, 05:06 AM
#15
Re: Struct vs Class
 Originally Posted by cilu
Only if that was so easy...  I would have done that with several professors when I was in college... 
Now I'm sorry I never accepted a professor after an interview, put him/her write/maintain "real-life" code to improve your college courses quality. 
Come on... they only taught you where to search... and I can presume this is one reason you found codeguru..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|