Are stucts faster than classes for linked lists?
I think stucts are easier to initialize becuase you dont need a constructor but its easier to have functions inside a class.
Also its a bit simpler to point to a struct than a class.
Printable View
Are stucts faster than classes for linked lists?
I think stucts are easier to initialize becuase you dont need a constructor but its easier to have functions inside a class.
Also its a bit simpler to point to a struct than a class.
structs are just classes with all public data. Neither is faster than the either. I don't see how its easier to point to a struct than a class.
I thought structs were better since they came from the C language. Someone once told me that C is better than C++. Oh well, veteran oppinions could mis guide you sometimes
C is not better than C++. C++ is not better than C. They are different, and each has it's own strong points. You don't see many new games being made in C, and you don't see any operating systems written in C++.
I think structs are easier to understand and write code with. I don't like classes too much, so I personally use structs for linked lists. Sometimes classes make things more complicated than they need to be.
c++ structs can have all the advanced features of c++ classes. They a basicly the same.
But we use them differently.
struct's and classes...mmm
I see no real diff if you are using c++, other than (already mentioned) struct's default data to public and classes default them to private. In c++ both can contain method's. If Im wrong correct me.
But personally it comes down to personal preference.
kwigibo
I don't think classes default to private, but they do have the ability to store private data. Structs cannot hold private data.
Actually classes are supposed to default to private access, and structs are and do default to public access.
Do this:
- define a structure
- declare an array of at least 2 elements
- in the program print out the addesses of each element in each structure
then:
- define a class
- declare an array with atleast 3 elements of the class
- in the program print out the address of each element in each class
From this, you should see that at run time the structure and the class are treated identically. The performance issues will be in function calls and how deep they are nested which you can control in the design.
sigma
Yes i see what you mean.
I guess the main difference is that a class uses a constuctor making the declaration different unless you declare all of its members public so you have dirrect access to them.
My big problem is finding a way to link a four dimensional list dynamically.
Currently Im doing it directly which would become very difficult when I add more elements to the list.
Well here is some advice, STRUCTS and CLASS's are different, yes they are, if you are using structs you typically use strict C code. (in professional code you do)
structs then cant have constructors or destructors, (HUGE Difference) and cannot have multipul inheritence.
now thats makes structs and class's different.
for linked lists tho, use structs, since its typically what is used in professional code.
by the way class's are defaulted to private, and structs are defaulted to public.. also structs can have private data members.
the guy who said they cant, do u code much?
I didn't know structs could have private data. I code a fair amount, you can check the CVS linked from my sig to see the code I've written.
Yeah, but I'm just learning c++ and I'm a sophomore in high school. I guess my teacher is wrong...
If you need a constructor, it would be better form to use a CLASS.
As a matter of preference, if you are going to embed functions into your objects- use a CLASS - for straight data members it's okay to use a struct.
How are u able to inherit from a STRUCT is something I've never heard of- inheritance and polymorphism are one of the major improvements of C++ over C.
I've never heard of STRUCTS holding private data and don't think it is possible unless someone wishes to show me otherwise.
Last thing, I don't think that CLASS's are defualted to private, they are defualted to public unless otherwise noted. This is becuase a constructor is public and if you were to create data member or member functions inside the CLASS without using specifications the access would be public. All functions and members would be accessible from outside the class. :o
Woohoo, maybe I'm not wrong. I completely agree with Rod.
Class defaults to private, constructors can be private/protected.
Private constructors are neccesary for the singleton design pattern.Code:struct blah {
private:
int a;
};
class blah2 {
blah2() { } // legal, private constructor
};
int main() {
blah inst;
// blah2 inst2; // illegal.. can't call private constructorf
return 0;
}
omg, i cant believe you people post before not testing it first:
here is an EXAMPLE of a private struct:
struct mystr{
private:
int ignorant;
public:
void set(int);
int get(void);
};
THERE YA GO A PRIVATIZED STRUCT.. so simple its hurting.
ROD PLEASE GO LEARN C++. And learn to read, i said structs CANNOT inherit.
ALSO, CLASS DEFAULT TO PRIVATE, YES THEY DO, UNLESS YOU SPECIFY IT TO BE PUBLIC. ITS PRIVATE!!!!!
geez people test ur code first.
Calm down there buddy.
I believe that it is better to have constructors public, (I hope u just forgot to type the public keyword). that's the way i do it. Im pretty sure compilers don't like private constructors, especially parameterized ones.
Code:class blah2 {
public: //Forgot this
blah2() { } // legal, private constructor
};
int main() {
blah inst;
return 0;
}
//*********************
Now put this bit of code into a simple program and try to access the integer variable inside each of them, and you'll soon find out the difference. You just won't be able to do it in a class.Code:
struct strone{
int a;
};
class clsone{
int a;
};
BTW use code tags when typing code
I was just showing that it was legal to have private constructors. There are real uses for private constructors... ala that singleton design pattern.
http://www.google.com/search?q=singleton+design+pattern
I've never used structs with private members.. usually structs have some minimal amount of implementation (perhaps a couple convienent constructors)and serve as little more than data aggregates.
On a unix platform, I can use the nm command to see the name space for data items and functions. If MS has a similair command, you will see that stuctures and classes are exactly the same after the code is compiled. This is because, C++ is backward compatible with C. When the compiler compiles a class, the resulting object code is the same.
So, ANYTHING, you can do in C++, can be done in C.
Why is that private? Can a constructor defined in the private section of a class be called like public construcors?Quote:
public: //Forgot this
blah2() { } // legal, private constructor
blah2 obj; //call constructor
obj = obj(); //calls constructor again
The keyword struct means different things in C++ than in C. It is used differently as well, although the general idea remains the same. I don't do much C programming, and I don't have my C reference here at work so I can't gaurantee the accuracy of the following, however, I should be pretty close:
Code://declaration of a struct type in C
typedef struct cStruct
{
int data;
char name[80];
//no functions possible in here
};
//declaration of a C struct variable in a program
int main()
{
struct cStruct myStruct;
//etc.
declaration of a struct type in C++
struct cppStruct
{
int data;
char name[80];
void showMembers();
};
//declaration of C++ struct variable in a program
int main()
{
cppStruct myStruct;
//etc.
in C++ the only difference between a struct and a class is that the members of a struct are public by default and the members of a class are private by default. Therefore you can have inheritiance of a struct in C++, just like you can with a class. It is not commonly done, because most programmers use structs in C++ similar to the way they are used in C out of convention/respect, but there is no inherent restriction in the language itself as far as I know. It should be easy enough to check.
Will try to compile at home when I have compiler available. For now code not tested by fire.Code:/*
program demonstrating inheritance using a struct, proving the members of a struct are public by default, and members of a class are private by default.
*/
#include <iostream.h>
struct Base
{
//all members default to public access
int bData;
Base();
};
struct BaseChild : public Base
{
//these are still public by default
BaseChild();
int showData();
//bData will be inherited as public by default as well.
//using private access for protection of data in a struct
private:
int bcData;
};
Base::Base() : bData(23)
{}
BaseChild::BaseChild()
{
bcData = 101;
}
int BaseChild::showData()
{
return bcData;
}
class MyClass
{
//using default private access on purpose
int cData;
public:
int showData();
};
MyClass::MyClass() : cData(4)
{}
int MyClass::showData()
{
return cData;
}
int main()
{
BaseChild baseChild;
MyClass myClass;
cout << "bData in baseChild is inherited, public and = "
<< baseChild.bData << endl;
//this line should cause compiler error so comment it out once
//it gives compiler error
cout << "bcData in baseChild using public access = "
<< baseChild.bcData << endl;
cout << "bcData in baseChild using public accessor function = "
<< baseChild.showData() << endl;
//this line should cause compiler error, too. so comment it out.
cout << "cData in myClass object is " << myClass.cData <<
endl;
cout << "using public accessor function cData in myClass is "
<< myClass.showData() << endl;
return 0;
}
Yup, once I cleaned up the code in my program a little bit, the errors predicted were present, and the code ran as expected, clearly demonstrating you can inherit from structs, that struct members default to public, and class members default to private.