Do u mean to say speed comes into picture?????Quote:
Originally Posted by manish_velankani
Printable View
Do u mean to say speed comes into picture?????Quote:
Originally Posted by manish_velankani
There is *no*, *none*, *zero* difference between a "struct" and a "class" except for the access specifiers.Quote:
Originally Posted by manish_velankani
Regards,
Paul McKenzie
Thanks!Quote:
Originally Posted by Paul McKenzie
Mr McKenzie,
i was hoping u could answer my post #41
Thanks in advance
I think one reason for keeping struct is for compatibility with C.
This is completely untrue for C++. In fact, the whole quotation does not apply to C++.Quote:
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types.
This could be be better asked the other way round:Quote:
Originally Posted by Pink Panther
So why the distinction????? Why the need of a class at all?????? They could have eliminated it totally from the C++, why keep it?????
For the earlier u got the common phrase as an answer "C- compatibility". U could use the code written in C, better say structs written in C from C++.
What's the answer for this question ? Why the need of an extra keyword "class"?
Cheers,
Exterminator
You can ask Bjorne Stroustrup why he didn't simply have struct and not bother with class.
Yes, you could write all your code using struct if you really wanted.
You are also allowed to do this in templates:
orCode:template < typename T >
(I don't know if template< struct T > would work, I've never tried it).Code:template < class T >
Anyway, the above are equivalent. Though I prefer to use typename in that situation.
structs and classes are "externally" exactly the same except for the default access specifiers.
Internally there is absolutely no reason to implement differently classes and structs, but compilers are allowed by the ANSI standard to do what they want internally.
A compiler can add big loops that do nothing in all class constructors and not add these big loops in struct constructors... That is legal, but of course it is really stupid, and it is absolutely sure that no compilers do that.
So, you must consider that classes and structs are the same, and if the compiler make a difference, than it is not your responsibility.
In fact, i see a point where a compiler may really treat differently structs and classes (but, i am sure that there is no compiler which acts like that):
The compiler may modify the declaration order of classes fields to have a better alignment (grouping 4-bytes fields at the start of the class, then 2-bytes fields, then 1-byte field).
And not modify this order for structs, to ensure that Win32 API is always correctly called, or any library written with another compiler.
The default access specifier is not a negligeable thing.Quote:
Originally Posted by exterminator
Forgetting to specify the access specifier is less dangerous with 'class' than with 'struct'.
Morevoer, the word 'class' conceptually better describes what it is.
And what follows is absolutely not official but is probable:
Many compilers are conforming to the ANSI (or are very near to be fully ANSI compliant).
But, also ensure a larger compatibility with old C compilers or C++ compilers (for example the "implementation" of structs is the same in many compilers).
And, adding a new 'class' keyword allow compilers to internally implement classes with more freedom, and restrict this freedom with structs to ensure compatibility for code that uses structs instead of classes.
So, if what i say is real, structs can only be slower or as fast as classes (because the compiler may take more freedom with classes than with structs, even if it is only a very implicit convention).
I already heard that the struct keyword is only here to ensure compatibility with C code and that C++ code should not use at all this keyword (at least if it does not interact with C modules).
Personally i use structs for very simple objects that cannot change, like the context parameter passed to the callback of enumerators, but maybe i should not do that.
Well...no...C structures are different from C++ structures. In both languages a structure is a collection of variables referenced under one name to keep related information together.Quote:
Originally Posted by YourSurrogateGod
This code is valid for C and C++. But the similarity stops right here. To declare a variable of type 'Structure' you would do the following:Code:struct Structure
{
int Integer;
float Float;
};
As you can see they are different. That's because in C you are NOT defining a new data type with the definition of a structure. Therefore you need to add the keyword 'struct' in front of the name of the structure when you declare variables of it.Code:// C
struct Structure structInfo;
// C++
Structure structInfo;
In C++, however, a new type is defined when a structure is defined, and you can use this new type to declare variables, and the struct keyword is NOT needed.
C++ structures are derived from C structures, therefore any C structure is also a valid C++ structure . However, C++ structures have some additional characteristics which C structures don't have. While C structures can contain variables only, a C++ structure can also contain functions. The following example would be a valid C++ structure but an invalid C one:
Code:struct Structure
{
int GetValue();
void SetValue(int Value);
private:
int Value;
};
I don't see how they can. Structs can do anything that classes can do: specifically, structs can contain member functions and they can inherit and be derived from.Quote:
Originally Posted by SuperKoko
is identical to:Code:class foo
{
public:
int bar;
int wibble;
};
How can a compiler know your intentions well enough to treat them differently? How can it know that you won't use class foo in a situation where a C function might reference it as a struct? The only thing it can do is to make a decision based on the actual definition of the type, not on whether it happens to use class or struct as the introduction.Code:struct foo
{
int bar;
int wibble;
};
Your professor is absolutely right: Structs are VALUE types and Classes are Reference Types and yes, there's a HUGE performance penalty if, for instance, you try to sort an array of objects containing structs because the whole array will be copied (value type), the sort will be performed and then returned to the calling method.
I absolutely don't understand why would some create a structure bigger than 16 bytes or so... It also has to do whether you have to allocate memory from the heap or the stack... etc... I don't want to go any further, but definitely stick to that professor. He seems to know the subject well.
Regards.
Your resurrect a 4 year old thread, just to ridicule yourself by babbling nonsense about a topic which you don't understand? I call that screwed up...