in Education by
This question already has answers here: Closed 10 years ago. 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 callback; int status; std::string message; Baton(Database* db_, Handle cb_) : db(db_), status(SQLITE_OK) { db->Ref(); uv_ref(uv_default_loop()); request.data = this; callback = Persistent::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 cb_, const char* filename_, int mode_) : Baton(db_, cb_), filename(filename_), mode(mode_) {} }; JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.

Related questions

0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: What are the differences ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Can a local variable's memory be accessed outside its scope? (20 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    let's say i have an interface as below public interface ConditionChecker { boolean isInCondition(Person p); ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    When should I use a struct rather than a class in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 10, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: C/Unix Strange behaviour while using system calls and printf (1 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to concatenate a std::string and an int (24 answers) Closed 5 years ago. int i = 4; string text = "Player "; cout...
asked Mar 24, 2022 in Education by JackTerrance
...