Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » stl list of structs

This thread is locked; no one can reply to it. rss feed Print
stl list of structs
Frank Drebin
Member #2,987
December 2002
avatar

until now i only used classes with stl::list.
when using a stl::list of mystruct the compiler gives me an error if i do

mystructs.push_back(); or mystructs.push_back(mystruct);

it seems i have to write

mystructs.push_back(mysrtuct());

is this ok?
why do i need the brackets here - a struct doens't have a construcor, right?

juvinious
Member #5,145
October 2004
avatar

Hmm, what?
you mean something like:

typedef struct myStruct
{
        int x;
        int y;
}MYSTRUCT;

list<MYSTRUCT>sList;
MYSTRUCT obj;
sList.push_back(obj);

__________________________________________
Paintown

Frank Drebin
Member #2,987
December 2002
avatar

juvinious
Member #5,145
October 2004
avatar

compiles for me

__________________________________________
Paintown

Archon
Member #4,195
January 2004
avatar

Quote:

a struct doens't have a construcor, right?

Isn't a struct in C++ just a class with default protection levels to public (IIRC from another thread in the past)?

Hano Wair
Member #5,243
November 2004
avatar

Quote:

Isn't a struct in C++ just a class with default protection levels to public (IIRC from another thread in the past)?

It is. :)

Frank Drebin
Member #2,987
December 2002
avatar

:o !
so how's a constructor defined in a struct (example)?
is there a destructor, too?

Archon
Member #4,195
January 2004
avatar

Quote:

so how's a constructor defined in a struct (example)?
is there a destructor, too?

Try defining it like you would define a class...

CGamesPlay
Member #2,559
July 2002
avatar

There is absolutely no difference between this line:struct A {And this line:class A { public:

[edit]
s/C/A/

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Frank Drebin
Member #2,987
December 2002
avatar

1#include <stdio.h>
2 
3struct test
4{
5 test() { printf("Constructor\n"); }
6 ~test() { printf("Destructor\n"); }
7};
8 
9 
10int main()
11{
12 test mytest; // works
13 // test mytest(); //doesn't work
14 
15 return 0;
16}

why aren't the *structors called when i put brackets after mytest?

CGamesPlay
Member #2,559
July 2002
avatar

Interesting. I'm sure it has something to do with this problem. The "fix" is this program:

1#include <stdio.h>
2 
3struct test
4{
5 test()
6 { printf("Constructor\n"); }
7 ~test()
8 { printf("Destructor\n"); }
9};
10 
11 
12int main()
13{
14 printf("no paren\n");
15 test mytest;
16 printf("paren\n");
17 test mytest2 = test();
18 printf("new\n");
19 test* mytest3 = new test();
20 delete mytest3;
21 
22 return 0;
23}

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

There is absolutely no difference between this line:

struct A {

And this line:

class C { public:

But A and C are different ?????????

CGamesPlay
Member #2,559
July 2002
avatar

Heh, interesting typo.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Indeterminatus
Member #737
November 2000
avatar

Quote:

// test mytest(); //doesn't work

It doesn't work because it's a forward declaration of a function with the name mytest.

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Matthew Dalrymple
Member #7,922
October 2006
avatar

Shouldn't this work though?

test *mytest = new test();

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

Archon
Member #4,195
January 2004
avatar

Quote:

Shouldn't this work though?


test *mytest = new test();

Yeah but that's using the heap memory.

X-G
Member #856
December 2000
avatar

Quote:

There is absolutely no difference between this line:

Wrong!

Structs have public inheritance by default. Classes do not.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Structs have public inheritance by default. Classes do not.

Could you post a code segment demonstrating the difference?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

X-G
Member #856
December 2000
avatar

1#include <stdio.h>
2 
3struct Primate {
4 int legs;
5};
6struct Monkey : Primate {
7};
8 
9class Vegetable {
10public:
11 int leaves;
12};
13class Tomato : Vegetable {
14};
15 
16int main(int argc, char **argv) {
17 Monkey steve;
18 Tomato pudgey;
19
20 printf("Steve has %d legs and Pudgey has %d leaves\n", steve.legs, pudgey.leaves);
21
22 return 0;
23}

gcc said:

classes.cpp: In function `int main(int, char**)':
classes.cpp:11: error: `int Vegetable::leaves' is inaccessible
classes.cpp:20: error: within this context

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Indeterminatus
Member #737
November 2000
avatar

Quote:

Structs have public inheritance by default. Classes do not. [emphasis added, ed.]

struct Base {
};

struct A : Base { // public inheritance by default
};

class WhatEverBase {
};

class A : public WhatEverBase { // not public by default
};

Granted, it doesn't make a difference in the sample you posted (as there's no inheritance whatsoever), but it still has to be considered a difference between a class and a struct (their default visibility aside).

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

CGamesPlay
Member #2,559
July 2002
avatar

Okay. That's what I thought, but I didn't know that struct A : Base was valid C++ :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Go to: