[solved]C++ Structures

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Martyj
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Apr 23, 2009 11:23 am
Location: Ogden Utah
Contact:

[solved]C++ Structures

Post by Martyj »

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.
Last edited by Martyj on Sun Jun 07, 2009 1:52 am, edited 1 time in total.
newbie1234
Chaos Rift Newbie
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

Post by newbie1234 »

They can hold variable data members. Unlike classes, they can't store any functions. Oh, and all members are set to public by default.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: C++ Structures

Post by K-Bal »

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.
In C++ they CAN hold functions. In C they don't.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

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).
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: C++ Structures

Post by Bakkon »

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.
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: C++ Structures

Post by Amarant »

Structs are only in C++ to provide backwards compatibility with C.
177
User avatar
Kros
Chaos Rift Regular
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

Post by Kros »

Amarant wrote:Structs are only in C++ to provide backwards compatibility with C.
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.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: C++ Structures

Post by K-Bal »

Kros wrote:
Amarant wrote:Structs are only in C++ to provide backwards compatibility with C.
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.
But you can achieve the same with a class since

Code: Select all

struct x
{
...
};
Is equivalent to

Code: Select all

class x
{
public:
...
};
and vice versa.

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++.
User avatar
Kros
Chaos Rift Regular
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

Post by Kros »

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++.
True, I stand corrected.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: C++ Structures

Post by avansc »

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();
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
wtetzner
Chaos Rift Regular
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

Post by wtetzner »

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();
Good point. You could maybe say structs in C can't have methods, but that still depends on how precisely "method" is defined. :)

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.
User avatar
dejai
Chaos Rift Junior
Chaos Rift Junior
Posts: 207
Joined: Fri Apr 11, 2008 8:44 pm

Re: C++ Structures

Post by dejai »

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).
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: C++ Structures

Post by K-Bal »

dejai wrote: 2. They are called methods when inside classes not functions.
Well, they are functions, so you can call them as such. You could also say member functions.
User avatar
programmerinprogress
Chaos Rift Devotee
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

Post by programmerinprogress »

K-Bal wrote:
dejai wrote: 2. They are called methods when inside classes not functions.
Well, they are functions, so you can call them as such. You could also say member functions.

I guess you could even call them Aardvarks, or sparrows, or potatoes, although any competant programmer would probably laugh at you... :lol:

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! :lol:
---------------------------------------------------------------------------------------
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
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: C++ Structures

Post by K-Bal »

The moral is:

don't use C# :lol:
Post Reply