-1

Possible Duplicate:
What are the differences between struct and class in C++
class and struct in c++

It looks like that struct can has constructor and destructor and members, and looks very simple, so can we use struct instead class, if not, when shall we use struct with functions when shall we use class ?

https://github.com/developmentseed/node-sqlite3/blob/master/src/database.h#L32

struct Baton {
    uv_work_t request;
    Database* db;
    Persistent<Function> callback;
    int status;
    std::string message;

    Baton(Database* db_, Handle<Function> cb_) :
            db(db_), status(SQLITE_OK) {
        db->Ref();
        uv_ref(uv_default_loop());
        request.data = this;
        callback = Persistent<Function>::New(cb_);
    }
    virtual ~Baton() {
        db->Unref();
        uv_unref(uv_default_loop());
        callback.Dispose();
    }
};

struct OpenBaton : Baton {
    std::string filename;
    int mode;
    OpenBaton(Database* db_, Handle<Function> cb_, const char* filename_, int mode_) :
        Baton(db_, cb_), filename(filename_), mode(mode_) {}
};
Community
  • 1
  • 1
guilin 桂林
  • 17,050
  • 29
  • 92
  • 146

7 Answers7

3

There's absolutely no technical reason to prefer one over the other, but I've noticed a certain convention regarding the use of class or struct.

If your datatype is something that is meant to be used by other parts of your program (ie. it's part of the 'interface'), then usually people make it a class to indicate its importance. If the datatype is used only in the implementation of a function or a class and it is not visible outside of a certain scope, then make it a struct.

These are some very rought guidelines, but no one will complain if you don't follow them.


Edit: In C++ there's no real difference between the two, but other newer languages that are inspired by C++ have actually made struct and class different. In C# and in D, for example, class and struct are both used to define datatypes, but they are not the same: struct is implemented such that it should be used for 'small' types.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
2

The only difference is the default access-level (private for a class, public for a struct). Other than that, they are completely interchangeable. You should decide which one you like better, and use that all the time (consistency makes your code more readable).

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
2

when shall we use struct with functions when shall we use class ?

It is completely your choice.
There is nothing that one can do with classes and not with structures in C++.

Only difference between structure and class are:

  • access specifier defaults to private for class and public for struct
  • inheritance defaults to private for class and public for struct

So just use the one of your choice and stick to using it consistently, do not mix classes and structures.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

The only difference between class and struct is the default accessibility to its members and base classes. For struct, it is public and for class, it is private.

fefe
  • 3,342
  • 2
  • 23
  • 45
1

While as stated by other struct & class does not have any difference besides default access level. However, it's common practice to use structs mostly for data aggregation, as that is what structs are reduced to in C. For example user defined PODs are almost always created as structs in my experience.

Ylisar
  • 4,293
  • 21
  • 27
0

As others have said, the main difference is the default access level of member data and functions, namely private for class and public for structs. The same goes for default inheritance access levels: private for classes and public for structs.

As for when to use which, that is a matter of what is normal for the company to do. In my experience, most companies, and indeed individuals, use structs to hold packets of pure data and classes for storing a collection of functions that operate on its own data and/or structs.

This method is a throwback to C programming where structs can only store data and not functions and so most people like to stick to this definition in C++ too.

Note that it is common to use structs for functors, which would seem to break consistency through the code of structs not containing functions, but since functors usually only overload the () operator we retain some form of consistency anyway. Plus, it saves us having to type public for one function and/or inherited structures... Oh the typing we allow ourselves to avoid ;)

David Brown
  • 522
  • 2
  • 12
-2

A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

Software_Designer
  • 8,490
  • 3
  • 24
  • 28