Page 1 of 1

[solved]C++ Structures

Posted: Thu May 28, 2009 3:00 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 5:24 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 6:20 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 6:46 am
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).

Re: C++ Structures

Posted: Thu May 28, 2009 8:58 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 9:10 am
by Amarant
Structs are only in C++ to provide backwards compatibility with C.

Re: C++ Structures

Posted: Thu May 28, 2009 9:15 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 9:26 am
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++.

Re: C++ Structures

Posted: Thu May 28, 2009 9:29 am
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.

Re: C++ Structures

Posted: Thu May 28, 2009 9:42 am
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();

Re: C++ Structures

Posted: Fri May 29, 2009 12:26 am
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.

Re: C++ Structures

Posted: Sun May 31, 2009 1:42 am
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).

Re: C++ Structures

Posted: Sat Jun 06, 2009 1:17 pm
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.

Re: C++ Structures

Posted: Sat Jun 06, 2009 8:08 pm
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:

Re: C++ Structures

Posted: Sat Jun 06, 2009 8:13 pm
by K-Bal
The moral is:

don't use C# :lol: