[solved]C++ Structures
Moderator: Coders of Rage
[solved]C++ Structures
In my online class of `oop with c++` we are learning about structures. What's the purpose of structures?
well this has been answered several times. You can continue to argue over the usefulness or lack of if you want.
well this has been answered several times. You can continue to argue over the usefulness or lack of if you want.
Last edited by Martyj on Sun Jun 07, 2009 1:52 am, edited 1 time in total.
-
- Chaos Rift Newbie
- Posts: 15
- Joined: Fri May 15, 2009 9:01 am
- Current Project: I'm currently making my own GUI
- Favorite Gaming Platforms: Err... Linux?
- Programming Language of Choice: C++
- Location: St. Petersburg, Russian Federation
Re: C++ Structures
They can hold variable data members. Unlike classes, they can't store any functions. Oh, and all members are set to public by default.
Re: C++ Structures
In C++ they CAN hold functions. In C they don't.newbie1234 wrote:They can hold variable data members. Unlike classes, they can't store any functions. Oh, and all members are set to public by default.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: C++ Structures
If you're writing things in C++ (instead of C), lots of programmers would argue that there is no purpose. They would argue that you should always use a class over a struct, because a struct's public nature violates the concepts of OO design. (I'm not one of them).
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: C++ Structures
Sometimes I use a struct for my node object when I'm writing a linked list. And every now and then I'll make a position struct to hold x, y, and z.
- Kros
- Chaos Rift Regular
- Posts: 136
- Joined: Tue Feb 24, 2009 4:01 pm
- Current Project: N/A
- Favorite Gaming Platforms: PC, Playstation/2/3
- Programming Language of Choice: C++
- Location: Oregon,USA
- Contact:
Re: C++ Structures
I don't agree. They're still fine to use when you want a simple object for containing data thats all public. Both of the examples Bakkon presented are good ones.Amarant wrote:Structs are only in C++ to provide backwards compatibility with C.
YouTube ChannelIsaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
Re: C++ Structures
But you can achieve the same with a class sinceKros wrote:I don't agree. They're still fine to use when you want a simple object for containing data thats all public. Both of the examples Bakkon presented are good ones.Amarant wrote:Structs are only in C++ to provide backwards compatibility with C.
Code: Select all
struct x
{
...
};
Code: Select all
class x
{
public:
...
};
So basically they are more or less two words for the same thing. So he is right, saying that structs are for backwards compatibility, because there is no need for a struct in C++.
- Kros
- Chaos Rift Regular
- Posts: 136
- Joined: Tue Feb 24, 2009 4:01 pm
- Current Project: N/A
- Favorite Gaming Platforms: PC, Playstation/2/3
- Programming Language of Choice: C++
- Location: Oregon,USA
- Contact:
Re: C++ Structures
True, I stand corrected.K-Bal wrote:So basically they are more or less two words for the same thing. So he is right, saying that structs are for backwards compatibility, because there is no need for a struct in C++.
YouTube ChannelIsaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
Re: C++ Structures
well saying structures cant have functions is a very iffy statement.
in C++ yeah sure they can. and in C... well yeah sure they can. its just that you have to have a pointer to a function. but nothing stops you from making a structure in C with a pointer to a function called foo and calling it.
struct->foo();
in C++ yeah sure they can. and in C... well yeah sure they can. its just that you have to have a pointer to a function. but nothing stops you from making a structure in C with a pointer to a function called foo and calling it.
struct->foo();
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: C++ Structures
Good point. You could maybe say structs in C can't have methods, but that still depends on how precisely "method" is defined.avansc wrote:well saying structures cant have functions is a very iffy statement.
in C++ yeah sure they can. and in C... well yeah sure they can. its just that you have to have a pointer to a function. but nothing stops you from making a structure in C with a pointer to a function called foo and calling it.
struct->foo();
And yeah, the only difference between structs and classes in C++ is that a struct's members are public by default and a class' are private by default.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
Re: C++ Structures
My Summary of the Above:
1. C Structures cannot hold functions however they can hold pointers to functions. (Sometimes seen as a stupid idea in C).
2. They are called methods when inside classes not functions.
As someone above said. Structs are alright for a wide variety of things. If you brush up on your C you will see the variety of uses it has in that language. However C++ originally called C with Classes pretty much made a new definition of structs called the class. The Class seems to do everything you want in C++ and in fact the definition of a public class is a struct. (Not the other way around seeing as structs were around before classes). And as C++ wanted to keep compatibility you thus have structs. If you don't like it learn D. (Its backend did get an open source port btw).
1. C Structures cannot hold functions however they can hold pointers to functions. (Sometimes seen as a stupid idea in C).
2. They are called methods when inside classes not functions.
As someone above said. Structs are alright for a wide variety of things. If you brush up on your C you will see the variety of uses it has in that language. However C++ originally called C with Classes pretty much made a new definition of structs called the class. The Class seems to do everything you want in C++ and in fact the definition of a public class is a struct. (Not the other way around seeing as structs were around before classes). And as C++ wanted to keep compatibility you thus have structs. If you don't like it learn D. (Its backend did get an open source port btw).
Re: C++ Structures
Well, they are functions, so you can call them as such. You could also say member functions.dejai wrote: 2. They are called methods when inside classes not functions.
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: C++ Structures
K-Bal wrote:Well, they are functions, so you can call them as such. You could also say member functions.dejai wrote: 2. They are called methods when inside classes not functions.
I guess you could even call them Aardvarks, or sparrows, or potatoes, although any competant programmer would probably laugh at you...
But one thing I must say is, when I started out programming (flicking between C++ and C# years ago now) I didn't know what a method was (a method is always a method in C# since it's fully OO), because I had learnt up to functions in C++, and I hadn't moved onto any OOP yet, so yeah, the term method baffled me in the C# book since they didn't bother explaining what it was, or comparing it to a function, it was baffling to say the least (but that was then, I know my functions from my methods from my templates from my classes now )
The moral of this short tale, don't be a dumbass like I was back in '07!
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P