Talk:C++ classes

From Wikipedia, the free encyclopedia
Jump to: navigation, search

predefined methods and operators[edit]

this article needs a short section on the behavior of predefined methods and operators (copy constructor, default constructor, default destructor, assignment operator). they are non-empty methods which might affect the usage of the class, and might need to be overwritten in some cases to implement proper behavior.

also, it would help to add information about implicit constructors and the explicit keyword. —Preceding unsigned comment added by (talk) 19:48, 30 January 2008 (UTC)

Bitwise operator controversy[edit]

The discussion of this controversy uses weasel words which weaken the argument. Deryck, can you cite sources for the two sides of this controversy? TheBoyNextDoor 01:47, 3 February 2006 (UTC)

structure?? class!![edit]

This article currently states that

"Syntactically, it[a struct] is analogous to a class in which member accessibility defaults to public."

There is nothing syntactic about this or analogous. A struct is exactly the same as a class, but with default accessibility public instead of private. --unsigned

I changed this wording a bit. It's more true or at least closer to common understanding now. I still don't like the oddly structure-centric view the rest of the page presents. --Tifego 04:20, 11 March 2006 (UTC)

This article discusses structs/classes, so naturally it also mentions members of those structs/classes, but operator overloading and templates are more indirectly related, so I think a more general name might be in order. Such as Object oriented programming in C++. Templates deserve their own page. They are not intrinsic to classes or OOP. Rather they are generic programming tools. -MarSch 11:49, 25 February 2006 (UTC)

Quite frankly, I feel this article's use of "struct" is wrong. The fact that structs in C++ are classes/objects with members that are public by default should not be of any real interest, unless you're writing code to parse C++ source. There's a clear conceptual difference between a struct and a class, and C++'s "structs" are not really structs at all. --StuartBrady 20:35, 4 March 2006 (UTC)

This is my source on the struct and class in C++ being nearly the same and this is about C++ right? C itself need not confuse things in an articale about C++ — Preceding unsigned comment added by Nerak99 (talkcontribs) 20:33, 22 September 2011 (UTC)

I wrote this article from the struct approach, explaining the usage (but not necessarily meaning) of structs (that means my intention is to write about structs instead of anything else through this article). As classes and structs have very similar properties, it's easy to confuse them. If you think that I have written about structs from the wrong approach, I would be happy to accept a rewrite, but at any rate I don't think we should keep the content but change the title without adding back another article here because there are numerous links to this article. Deryck C. 07:29, 5 March 2006 (UTC)

There are only 6 non-talk, non-redirect links. I personally feel it's a great article, when reading "class" in place of "struct". However, I would argue that this article should be renamed regardless of any new article to replace it. --StuartBrady 13:05, 5 March 2006 (UTC)

However, I must say that the usage of a struct is correctly demonstrated in the article. It is just another fact that a class is a better way to do the above functions. So maybe we can write ANOTHER article to talk about classes, keeping this existing struct article and expanding it with some struct-specific properties. Anyway I don't agree a title change to this article. Deryck C. 04:37, 6 March 2006 (UTC)

I think the article should become slightly more general so that its topic is both classes and structures, perhaps with an added section to address the issue of when it is commonly considered appropriate to use a structure instead of a class. C++ class ought to redirect to this article even if this article isn't renamed. And templates already have their own page, although the section on Templates here contains some information about them that's not covered there. --Tifego 04:20, 11 March 2006 (UTC)
I just think that classes and structs should have separate articles, because the syntax of classes is a lot more complex than structs. In fact from the beginning of this "class or struct" discussion I'm waiting sb to start another article on classes. Deryck C. 16:10, 11 March 2006 (UTC)
"syntax of classes is a lot more complex than structs"? Could you give some examples? I can't think of a single one. --Tifego 16:49, 11 March 2006 (UTC)

Classes have what "static void", "virtual", "abstract" etc... that I've never heard of their existence in structs. Deryck C. 14:40, 12 March 2006 (UTC)

"static void" is made up of 2 different keywords, there's nothing special about them being together and it's perfectly legal for a struct to have a function defined with it. Abstract isn't a keyword in C++, unless you were referring to pure virtual (=0) functions (which should be mentioned in the article if they aren't already, btw). As for "virtual"... I don't believe there is anything preventing its use in structs - I've never tried it myself because I've been told it's bad form, but that's more a matter of convention than a property of the language. --Tifego 03:25, 13 March 2006 (UTC)

When I write structs, I only have to put in the members, add some functions, overload the less-than, than's all. It's sort of WYSIWYG. But for classes, we need some sort of "derivative members", "public, private and protected" that complicate things up. But I'd be very glad if you can tell me the real difference between a struct and a class so that I can try it out myself and gain some more experience. --Deryck C. 03:29, 13 March 2006 (UTC)

I believe there are only 2 differences between C++ structs and C++ classes:
  1. Structs begin with public member access to things inside, whereas classes begin with private access. Simply add "public:" or "private:" or "protected:" in the class or struct to eradicate this difference.
  2. The struct and class keyword can't be freely mixed, so you can't call something a struct and then later refer to it as a class. This isn't a difference between what a struct and a class is, really, just a syntax note.
--Tifego 03:37, 13 March 2006 (UTC)

Do you mean that structs and classes have only two differences:

  1. Structs default to public, classes default to private. This can be changed by adding public, private and protected keyword to any of them.
  2. They differ in the name

And by convention one would not mix them up, although they are simply equal except the above-said two differences? --Deryck C. 04:00, 13 March 2006 (UTC)

Yes, but what do you mean by "by convention one would not mix them up"? It will actually fail to compile if you refer to something as a class in one part of the code and a struct in another part of the code. The convention is that structs should only be used in place of classes when the class is relatively simple or would have its objects treated mostly as if it were a C struct (i.e. when it's a data container with items that are manipulated mainly by direct access from non-member functions). --Tifego 04:18, 13 March 2006 (UTC)

Referring to your claim that It will actually fail to compile if you refer to something as a class in one part of the code and a struct in another part of the code - I wonder if the struct or class keyword for the same user-defined variable would appear twice (or more) within the same code passage? Deryck C. 04:26, 13 March 2006 (UTC)

I meant like this:
// gives error
class A;
struct A {int i;};

// compiles
class A;
class A {int i;};

// gives error
void func(struct A* a);
class A {int i;};

// compiles
void func(class A* a);
class A {int i;};

// gives error
void func(class A* a);
class A {int i;};
func(struct A* a) {}

// compiles
void func(struct A* a);
struct A {int i;};
func(A* a) {a->i = 0;}
--Tifego 05:10, 13 March 2006 (UTC)

I don't quite understand. The first one is a prototype, right? But how about the others? Don't we just declare functions like A* a, A a, and omit the words struct or class? --Deryck C. 06:17, 13 March 2006 (UTC)

Well, you can't just omit the words if you haven't defined what A is beforehand, C++ doesn't compile the file out-of-order like that.
// won't compile
void func(A* a);
class A {int i;};
Anyway, the other examples are like prototypes too, it's just putting it in a slightly different place (to save space). The above examples could have that syntax replaced with no change:
// example 1 same
// example 2 same

// gives error
struct A;
void func(A* a);
class A {int i;};

// compiles
class A;
void func(A* a);
class A {int i;};

// gives error
class A;
void func(A* a);
class A {int i;};
func(struct A* a) {}

// compiles
struct A;
void func(A* a);
struct A {int i;};
func(A* a) {a->i = 0;}
The prototype "struct A;" basically just replaces (by typedef) all occurences of "A" with "struct A" after that (until it becomes defined as something more precise), which is why it works to skip the prototype and directly use "struct A" in place of "A" at the places where A is not yet known to be a struct. (This is all only for pointers to A, not references or objects of type A, which the compiler can't possibly use without knowing what they are.) The ability to "prototype" classes like this (using either way of doing the syntax, that doesn't matter) is a useful feature of the language and often absolutely necessary, because it's the only way to allow for circular references between classes. A lesser benefit is that it allows you to avoid including files that define a class just to use pointers to that class, which can save a lot of compile-time over the course of a reasonably large-scale project. --Tifego 06:57, 13 March 2006 (UTC)

Totally understand. So it's time to have some collaboration on a new article, C++ class. --Deryck C. 07:41, 13 March 2006 (UTC)

I think there's not enough material that would be different for C++ class vs. C++ structure. Better to have them both go to the same page which talks about both, since they're so similar. Also, you've seen class (computer science), right? --Tifego 07:48, 13 March 2006 (UTC)

Don't quite understand the "virtuals" and the inheritance. What does class concrete : public Abstract mean? --Deryck C. 08:41, 13 March 2006 (UTC)

I don't get any compile error on gcc-3.4.5 for
struct A;
void func(A* a);
class A {int i;};

int main()
        struct A i;
        class A j;
        A k;

so the only difference between a class and a struct is the default member-accessibillity, public for a struct and private for a class. Obviously this "difference" does NOT merit two articles. What we could have is an article on C structs and one on C++ class with a redirect from C++ struct to C++ class. What do you think? -MarSch 12:12, 5 April 2006 (UTC)

Possibly unfortunately, I created C++ class to be a redirect to this article. Then again, this article would be a bit strange if it were titled "C++ class" without its contents being changed to refer to classes instead of structs for most of the article.

I'm not sure what that example was there to prove. I didn't know that would work, but it doesn't change the fact that the struct and class keywords aren't literally interchangeable (you can't cast a struct A* to a class A* when only a struct A type has been defined, for instance).

Tifego(t) 07:15, 27 April 2006 (UTC)

I've just successfully compiled the following code in GCC 4.0:

struct A { int i; };

    A *a            = new A;
    struct A *a2    = (class A*) a;     // unnecessary but valid cast
    class A *a3     = (class A*) a;     // another
    class A *a4     = (struct A*) a;    // and another

So, it seems struct and class are basically interchangeable, except for the difference in their default member accessibility. JCarlos 19:13, 21 October 2006 (UTC)

This discussion is very old, but in my opinion, the article still needs to be clearer on the difference (or non-difference) between structs and classes. I have therefore tagged the article. decltype 12:59, 19 January 2009 (UTC)

You can find the a trustworthy explaination in here: You should fix the first section as it is misleading. (talk) 21:35, 13 October 2009 (UTC)


Two of the controversies (shift operators and string operators) are really just issues with operator overloading; they're mentioned in the "Criticisms" section of that article. Also, I don't see how these "controversies" are related to structs in C++. The two I mentioned are more accurately held against operator overloading , regardless of the language. And the remaining one ("Extra keywords") is to do with C++'s syntax in general, and not any specific language feature. -- destrius 19:38, 27 March 2006 (UTC)

It's true that they're not struct-specific. I put them there because they're controversies related to structs. --Deryck C. 14:49, 25 April 2006 (UTC)
The "Controvercy" header should be moved to a more appropriate article as they are unrelated to the article topic. Mikademus 09:46, 14 August 2006 (UTC)
It's a Wiki, you just do it. That section was utterly irrelevant to structs, and was packed with juicy weasel words.-- Rogerborg 12:21, 28 November 2006 (UTC)

incompatible with C[edit]

This sentence is currently in the article:

"The C++ struct declaration is incompatible with its C counterpart."

What is meant here? "C structs can't be used in C++", or "C++ structs can't be used in C"? I read it as the former, and I think that, while it might be technically correct, it is a misleading statement. As far as I know, the only incompability is when a C++ keyword is used as an identifier. I think this sentence should be removed or rephrased to reflect this. Am I wrong? Ahy1 16:17, 25 April 2006 (UTC)

You're correct, because the sentence is meant to mean the latter. I'd try to rephrase this. --Deryck C. 16:28, 26 April 2006 (UTC)

Collaboration suggestion[edit]

I suggest that we collaborate in rewriting the intro section to cater for the differences between "struct" and "class", and then rename the article "C++ structures and classes". The examples can be kept as class and struct works basically the same way. However, in cases which member accessibility is concerned, additional paragraphs should be added. Before, or after that, we can have a separate section on the differences between struct and class. (the language's bit clumsy here - not feeling well today) --Deryck C. 09:19, 24 October 2006 (UTC)

Can anybody write a paragraph about public and private accessibility in this article? --Deryck C. 07:03, 30 October 2006 (UTC)
Remember, inheritance is different as well. -- Rogerborg 12:22, 28 November 2006 (UTC)

Factual accuracy[edit]

I have removed the tag for the entire article, since the largest problem has been corrected. There's still problems in certain sub-sections, though. In particular, a lot of implementation details that vary from compiler to compiler are presented as being universally true for C++ in general. decltype 12:15, 20 January 2009 (UTC)

"Inheritance" section cleaned up somewhat[edit]

I re-worded the Inheritance section, and reworked the example to make it clear that there's no guarantee on the memory layout of classes, in the general case. I left the example of how it's usually done, in the simple case, because it seems useful.

A definite improvement. Good work! decltype (talk) 09:21, 25 September 2009 (UTC)

Terminology: class declaration vs definition[edit]

According to the C++ standard section 9.2 (I'm reading a draft), class is declared immediately after the class-name is seen, and is considered defined after the closing brace of its class-specifier has been seen even though its member functions are in general not yet defined. That is:

// Class declaration
class example;
// Class definition
class example {
    // Member function declaration
    void member();
// Member function definition
void example::member() {

Given this, should we change declaration -> definition in section 2 of the article, as the examples there are both declarations and definitions? Kem wiki (talk) 10:20, 26 March 2010 (UTC)

Class naming conventions[edit]

I learned that classes should start with uppercase characters. The examples in this text are not made according to the convention. I looked it up and the convention is listed there. Maybe there is some other reason for the minor error, but I've adjusted the article with one sentence. Please correct me if I'm wrong. —Preceding unsigned comment added by (talk) 11:54, 22 June 2010 (UTC)

The C++ Standard Library and the Standard Template Library, which usually comes with c++, follows the convention that class names consist of only lowercase characters and underscores. (For example: priority_queue) I suppose the examples follow this convention. --Bcmpinc (talk) 12:01, 4 October 2010 (UTC)
I agree with Bcmpinc: lowercase underscored is the standard for the STL. While many companies standardize on capitalized CammelCase for class names, it's just a convention. To my eye it is more "right" to have C++ examples on Wikipedia mirror the conventions used by the C++ standards group. —Ben FrantzDale (talk) 12:31, 4 October 2010 (UTC)

Memory consumption example is wrong[edit]

In example it is shown that, after two char two spaces will be used for padding. And after one short int another two space will be used for padding. If we assume this argument, then structure's size will be 20 (as 1+1+2 + 2+2 +4 + 8 = 20 bytes). But it takes only 16. As we can arrange two car and one short int together to get only 4 bytes and there will be no need of padding. So total size of structure will total up to only 16. proving no padding is used. So, please change that example. Thank.

Attached a link to Codepad for given exapmle showing 16 as output for sizeof. —Preceding unsigned comment added by (talk) 06:15, 7 December 2010 (UTC)

Why is there struct people?[edit]

on the example of both struct and class, after struct person, there is a struct people. Why? This makes the two inequivalent. —Preceding unsigned comment added by (talk) 22:53, 4 March 2011 (UTC)

Needs MOD ATTENTION due to gross errors[edit]

The article starts

<<The C++ programming language allows programmers to separate program-specific datatypes through the use of classes. Classes define types of data structures and the functions that operate on those data structures. Instances of these datatypes are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer. Syntactically, classes are extensions of the C struct, which cannot contain functions or overloaded operators.>>

The C++ standard defines an "object" as a "region of storage", not necessarily of class type.

A C++ object does not "contain" member functions or overloaded operators.

It is not true in C++ that a `struct` cannot define functions or overloaded operators.

And so on: this article seems to be written by persons with absolutely no knowledge of C++, and no inclination to consult reference works (in particular the C++ standard). — Preceding unsigned comment added by (talk) 05:27, 27 August 2012 (UTC)

And you are more than welcome to edit the article text yourself to correct perceived errors. However, I do not see a problem with the term object the way it is used in the lede. Yes, there are objects which do not have class type, but instances of classes are still objects. Objects containing member functions is definitely an inaccuracy in the article text. Regards, decltype (talk) 08:17, 28 August 2012 (UTC)

Need new redirect[edit]

"cpp classes" should redirect to this page

Added.SDavidJacobsen (talk) 15:35, 14 September 2015 (UTC)

In-class initializers not allowed in an aggregate type[edit]

According to the chapter "Class Features" of the book "C++11/14 Rocks!" by Alex Korban, a type cannot be an aggregate if it has in-class initializers. This fact should be included in the section on aggregate classes. I didn't feel qualified to choose the best reference for this point and modify the existing reference link, so I'm hoping some else will do that. --AlanUS (talk) 13:56, 18 May 2016 (UTC)