Hi facepunchers.
I'm learning more about structs and classes and found out that you can do something like:
[code]
struct kNumber
{
// A default constructor
kNumber() {}
kNumber(float X)
{
data = X;
}
// + operator
kNumber operator+(kNumber vVector)
{
return kNumber(vVector.data + data);
}
float data;
};
[/code]
and then use it
[code]
kNumber num1 = 4.23;
kNumber num2 = 5;
kNumber num3 = num1 + num2;
cout << num3.data << endl;
[/code]
and then we print out 9.23 as expected but is it possible to access .data by just calling num3 ? I'm probalby just dumb now but I mean that's what you're doing with the string class, right?
[code] cout << num3 << endl;[/code]
You could do the following:
[cpp]float operator float() { return data; }[/cpp]
Don't do the "C/C++" thing. C structs are totally different than C++ structs. What you've written is not even close to valid C.
[QUOTE=Overv;24820128]You could do the following:
[cpp]kNumber operator float() { return data; }[/cpp][/QUOTE]
Thank you :)
[code]
operator float() { return data; }
[/code]
[QUOTE=ROBO_DONUT;24820173]Don't do the "C/C++" thing. C structs are totally different than C++ structs. What you've written is not even close to valid C.[/QUOTE]
Okaj, I understand. So constructor is not valid C?
[QUOTE=likesoursugar;24820292]Okaj, I understand. So constructor is not valid C?[/QUOTE]
Pretty much none of that is valid C.
You can't have methods in a struct, you can't overload functions or operators, and you can't have constructors (because you can't have methods).
In C, a struct is just a collection of values. In my opinion, this is significantly more useful than the C++'s redundant class-like structs.
I see! It's so messy for me sometimes. But now it's very clear for me. thanks you again.
Why should you use struct for something more than just storing values. class is much better.
[QUOTE=sim642;24820799]Why should you use struct for something more than just storing values. class is much better.[/QUOTE]
There is literally no difference but a few minor visibilty/inhertiance shit.
[QUOTE=ROBO_DONUT;24820336]In C, a struct is just a collection of values. In my opinion, this is significantly more useful than the C++'s redundant class-like structs.[/QUOTE]
Actually, in C++ a struct without methods is just a struct. I believe it doesn't even have a vtable like classes do. (The vtable is the place where the methods are stored. Or something like that)
[editline]06:05PM[/editline]
[QUOTE=Pirate Ninja;24820815]There is literally no difference but a few minor visibilty/inhertiance shit.[/QUOTE]
There is literally no difference apart from the fact that struct members are by default public and class members private.
[QUOTE=esalaka;24821560]I believe it doesn't even have a vtable like classes do. (The vtable is the place where the methods are stored. Or something like that)[/QUOTE]
It doesn't have a vtable, because it's used by classes to store the addresses of virtual functions. And since structs don't have inheritance, vtables aren't necessary.
[QUOTE=esalaka;24821560]Actually, in C++ a struct without methods is just a struct. I believe it doesn't even have a vtable like classes do. (The vtable is the place where the methods are stored. Or something like that)[/QUOTE]
The vtable exists in every class or struct in which virtual functions are present. The vtable contains pointers to only the virtual functions.
[QUOTE=esalaka;24821560]There is literally no difference apart from the fact that struct members are by default public and class members private.[/QUOTE]
Also, by default inheritance for classes is private and for struct is public, which comes from the default visibility though.
[editline]05:40PM[/editline]
[QUOTE=Overv;24820128]You could do the following:
[cpp]float operator float() { return data; }[/cpp][/QUOTE]
Wouldn't overloading the operator<< the proper thing to do?
[QUOTE=Overv;24821853]It doesn't have a vtable, because it's used by classes to store the addresses of virtual functions. And since structs don't have inheritance, vtables aren't necessary.[/QUOTE]
Nice to learn new stuff.
Where are the concrete methods, then?
[QUOTE=esalaka;24822172]Nice to learn new stuff.
Where are the concrete methods, then?[/QUOTE]
They are linked at compile time, they have no presence in the instantiated object. The vtable exists to determine which virtual method to call at run-time.
Methods that cannot be overridden in child classes/structs.
[QUOTE=esalaka;24821560]Actually, in C++ a struct without methods is just a struct. I believe it doesn't even have a vtable like classes do. (The vtable is the place where the methods are stored. Or something like that)[/QUOTE]
Is that part of the actual standard, or is it just a common convention? Although the C standard doesn't explicitly define a lot of things about structs (alignment, etc), there are some properties you can pretty much rely on because 95% of code would break otherwise. I'm more than a little uncomfortable making the same assumptions with C++ because structs are not on the same level of importance and generally not used in the same manner.
An idiom of C++ is that you don't have to pay for features you don't use.
So, if you don't use virtual functions, then you won't get a vtable in that class/struct.
The vtable is also a static member and the instance will just have a pointer to it.
I'm not sure if this is defined by the standard to be as such, but at least it is the usual implementation.
[editline]06:54PM[/editline]
[url]http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.3[/url]
It's to be up to the compiler how it handles virtual functions.
[QUOTE=ZeekyHBomb;24823071]An idiom of C++ is that you don't have to pay for features you don't use.
So, if you don't use virtual functions, then you won't get a vtable in that class/struct.[/QUOTE]
An idiom is not exactly reassuring when you need to depend on certain behavior for your program to not fail catastrophically.
[QUOTE=ZeekyHBomb;24823071]The vtable is also a static member and the instance will just have a pointer to it.[/quote]
A single pointer is still enough to cause major problems when parsing raw binary data.
[QUOTE=ZeekyHBomb;24823071]It's to be up to the compiler how it handles virtual functions.[/QUOTE]
So there's the answer. If you want to write portable code, you can't use C++ structs like you'd use C structs. This bothers me because C++ structs don't have any apparent purpose. It's just like writing "class {public: /*stuff*/}", but maybe a few characters shorter.
That and partial (dunno how much) C-compatibility.
Also, in C structs you won't find virtual functions like in C++ and as such there won't be a static vtable, nor a pointer, nor anything else that would be needed for overloading.
[QUOTE=ZeekyHBomb;24822054]Wouldn't overloading the operator<< the proper thing to do?[/QUOTE]
By making a cast operator, he can do a lot more than just printing the value.
But sometimes it might not be logically correct to be able to cast something into a value, when you just wanna be able to write something as a value.
Overloading the shifting operator as streaming operator also gives you more ways to print stuff like more complex classes that have more than just one number.
[QUOTE=ZeekyHBomb;24823893]But sometimes it might not be logically correct to be able to cast something into a value, when you just wanna be able to write something as a value.
Overloading the shifting operator as streaming operator also gives you more ways to print stuff like more complex classes that have more than just one number.[/QUOTE]
In my opinion, overloading the shift operator makes even less sense for things with numerical values. I know it's common practice in C++ to use arithmetic operators for non-arithmetic purposes (IOStreams, etc.), but it still makes me :psyduck:.
Well, you can still do
[cpp]MyClass instance;
instance >> 4; //right shift
std::cout << instance; //stream[/cpp]
By overloading the first one as a proper shifting operator and the second one for streaming.
It will decide by the type which function to call.
[QUOTE=Overv;24821853]It doesn't have a vtable, because it's used by classes to store the addresses of virtual functions. And since structs don't have inheritance, vtables aren't necessary.[/QUOTE]
No. Structs in C++ do have inheritance.
The only difference between a struct and a class in C++ is that struct members and methods are public by default, in classes the default is private.
[editline]01:27AM[/editline]
[QUOTE=ROBO_DONUT;24823388]
So there's the answer. If you want to write portable code, you can't use C++ structs like you'd use C structs. This bothers me because C++ structs don't have any apparent purpose. It's just like writing "class {public: /*stuff*/}", but maybe a few characters shorter.[/QUOTE]
Structs in C++ are mainly for backwards-compatibility purposes, and you can use them like C structs by convention.
And they're just as "portable" as C structs, because the C++ standard defines that structs and classes can be [url=http://en.wikipedia.org/wiki/Plain_old_data_structure]Plain Old Data[/url] if they meet certain conditions. In these conditions, the struct or class behaves just like they would in C (which isn't very portable without pragmas like struct packing and stdint etc).
[QUOTE=jA_cOp;24842105]And they're just as "portable" as C structs, because the C++ standard defines that structs and classes can be [url=http://en.wikipedia.org/wiki/Plain_old_data_structure]Plain Old Data[/url] if they meet certain conditions. In these conditions, the struct or class behaves just like they would in C[/QUOTE]
Didn't know that, thanks.
[QUOTE=jA_cOp;24842105](which isn't very portable without pragmas like struct packing and stdint etc).[/QUOTE]
They can be written with portability in mind. As I mentioned above, padding isn't explicitly defined in C, but there are certain patterns. You write for four-byte alignment. Four-byte alignment is common and a four-byte-aligned struct will work equally well with two-byte alignment and no alignment. So while "int char int" is going to be unpadded on some systems, but padded on others "int char char char char int" should work fine almost all the time. No need for pragmas.
I think that a lot of old binary formats are written with four-byte alignment in mind, so you shouldn't really run into this issue most of the time.
stdint is a very useful tool, and there's really no reason not to use it, so I don't know why you'd be talking about structures "without stdint".
[QUOTE=ROBO_DONUT;24842427]I don't know why you'd be talking about structures "without stdint".[/QUOTE]
Because stdint isn't universally supported yet for god-knows-what reason. It's a problem you run into and have to deal with (like people using MSVC including a custom stdint.h or cstdint header with their projects).
[QUOTE=jA_cOp;24842511]Because stdint isn't universally supported yet for god-knows-what reason. It's a problem you run into and have to deal with (like people using MSVC including a custom stdint.h or cstdint header with their projects).[/QUOTE]
There's too many problems with compiling C code on MSVC to even list, so it's really not worth supporting. stdint.h can be provided by the user if it isn't available. You only really need to declare the eight main types most of the time (signed/unsigned 8/16/32/64-bit) and can be easily hand-written if need be.
Also, a few things I'd like to add to the alignment/padding issue. I acknowledge that it's an annoyance, but they're fixing it in the upcoming C1X standard by adding alignment specifiers. I don't really have too much of a problem with padding/alignment as it is now because the inconsistencies are the most often the result of the hardware architecture of the platform you're working on, not determined arbitrarily by the compiler. I'm fine with this because most programmers should know something about their hardware.
[QUOTE=ROBO_DONUT;24842694]There's too many problems with compiling C code on MSVC to even list, so it's really not worth supporting. stdint.h can be provided by the user if it isn't available. You only really need to declare the eight main types most of the time (signed/unsigned 8/16/32/64-bit) and can be easily hand-written if need be.
[/QUOTE]
It's worth it for C++. (It is also part of the most recent C++ standard)
[QUOTE=jA_cOp;24842713]It's worth it for C++. (It is also part of the most recent C++ standard)[/QUOTE]
Yeah, it might be worth it, but I probably wouldn't bother anyway. I can be a bit of an unreasonable FOSS nut.
If youre only getting and setting stuff use a structure. Soon as you want to manipulate the data, use a class. Theres really no formula to use. Just do what you think is right. If youre a beginner, stick to using classes.
Sorry, you need to Log In to post a reply to this thread.