All Questions
15,890
questions
1268
votes
27
answers
623k
views
When should you use a class vs a struct in C++? [duplicate]
In what scenarios is it better to use a struct vs a class in C++?
634
votes
18
answers
291k
views
Does the 'mutable' keyword have any purpose other than allowing a data member to be modified by a const member function?
A while ago, I came across some code that marked a data member of a class with the mutable keyword. As far as I can see it simply allows you to modify a member in a const-qualified member method:
...
530
votes
11
answers
495k
views
How can you define a static data member of type const std::string?
I'd like to have a private static constant for a class (in this case a shape-factory).
I'd like to have something of the sort.
class A {
private:
static const string RECTANGLE = "...
497
votes
29
answers
384k
views
What are the differences between struct and class in C++?
This question was already asked in the context of C#/.Net.
Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for ...
361
votes
19
answers
597k
views
What is the difference between private and protected members of C++ classes?
What is the difference between private and protected members in C++ classes?
I understand from best practice conventions that variables and functions which are not called outside the class should be ...
321
votes
19
answers
217k
views
What is a pointer to class data member "::*" and what is its use?
I came across this strange code snippet which compiles fine:
class Car
{
public:
int speed;
};
int main()
{
int Car::*pSpeed = &Car::speed;
return 0;
}
Why does C++ have this ...
314
votes
15
answers
584k
views
How do you create a static class?
How do you create a static class in C++? I should be able to do something like:
cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl;
Assuming I created the BitParser ...
252
votes
8
answers
461k
views
How to separate a class and its member functions into header and source files
I am confused on how to separate implementation and declarations code of a simple class into a new header and cpp file. For example, how would I separate the code for the following class?
class A2DD
{...
242
votes
7
answers
72k
views
Forward declaration of nested types/classes in C++
I recently got stuck in a situation like this:
class A
{
public:
typedef struct/class {…} B;
…
C::D *someField;
}
class C
{
public:
typedef struct/class {…} D;
…
A::B *someField;
}
...
192
votes
4
answers
246k
views
How to create a template function within a class? (C++)
I know it's possible to make a template function:
template<typename T>
void DoSomeThing(T x){}
and it's possible to make a template class:
template<typename T>
class Object
{
public:
...
182
votes
31
answers
150k
views
Why use prefixes like m_ on data members in C++ classes?
A lot of C++ code uses syntactical conventions for marking up data members. Common examples include
m_memberName for public members (where public members are used at all)
_memberName for private ...
176
votes
5
answers
326k
views
Declaring an enum within a class
In the following code snippet, the Color enum is declared within the Car class in order to limit the scope of the enum and to try not to "pollute" the global namespace.
class Car
{
public:
enum ...
160
votes
6
answers
174k
views
Unresolved external symbol on static class members
Very simply put:
I have a class that consists mostly of static public members, so I can group similar functions together that still have to be called from other classes/functions.
Anyway, I have ...
136
votes
7
answers
125k
views
C/C++ Struct vs Class
After finishing my C++ class it seemed to me the structs/classes are virtually identical except with a few minor differences.
I've never programmed in C before; but I do know that it has structs. In ...
136
votes
3
answers
305k
views
Creating an instance of class
What's the difference between lines 1 , 2 , 3 , 4?
When do I use each?
Why line 3 prints the constructor Foo and line 7 returns an error and line 8 doesn't?
#include <iostream>
using ...
121
votes
17
answers
76k
views
C++: What is the size of an object of an empty class?
I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and point to it like any other object. But, how big is ...
116
votes
16
answers
90k
views
Does C++11 have C#-style properties?
In C#, there is a nice syntax sugar for fields with getter and setter. Moreover, I like the auto-implemented properties which allow me to write
public Foo foo { get; private set; }
In C++ I have to ...
114
votes
15
answers
203k
views
How do I check if an object's type is a particular subclass in C++?
I was thinking along the lines of using typeid() but I don't know how to ask if that type is a subclass of another class (which, by the way, is abstract)
114
votes
3
answers
9k
views
Is passing a C++ object into its own constructor legal?
I am surprised to accidentally discover that the following works:
#include <iostream>
int main(int argc, char** argv)
{
struct Foo {
Foo(Foo& bar) {
std::cout <&...
113
votes
5
answers
81k
views
How can I use cout << myclass
myclass is a C++ class written by me and when I write:
myclass x;
cout << x;
How do I output 10 or 20.2, like an integer or a float value?
112
votes
16
answers
116k
views
Splitting templated C++ classes into .hpp/.cpp files--is it possible?
I am getting errors trying to compile a C++ template class which is split between a .hpp and .cpp file:
$ g++ -c -o main.o main.cpp
$ g++ -c -o stack.o stack.cpp
$ g++ -o main main.o stack.o
...
105
votes
10
answers
279k
views
How can I get the class name from a C++ object?
Is it possible to get the object name too?
#include<cstdio>
class one {
public:
int no_of_students;
one() { no_of_students = 0; }
void new_admission() { no_of_students++; }
};
int ...
103
votes
3
answers
118k
views
C++11 allows in-class initialization of non-static and non-const members. What changed?
Before C++11, we could only perform in-class initialization on static const members of integral or enumeration type. Stroustrup discusses this in his C++ FAQ, giving the following example:
class Y {
...
102
votes
4
answers
343k
views
Expression must have class type
I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet:
class A
{
public:
void f() {}
};
int main()
{
{
A a;
a.f(); // works fine
}
{
...
99
votes
5
answers
125k
views
Class template with template class friend, what's really going on here?
Let's say I'm creating a class for a binary tree, BT, and I have a class which describes an element of the tree, BE, something like
template<class T> class BE {
T *data;
BE *l, *r;
...
97
votes
8
answers
52k
views
Semicolon after class declaration braces
In C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do ...
92
votes
9
answers
125k
views
C++ Abstract Class: constructor yes or no?
A class with one (or more) virtual pure functions is abstract, and it can't be used to create a new object, so it doesn't have a constructor.
I'm reading a book that provides the following example:
...
88
votes
6
answers
207k
views
Define a struct inside a class in C++
Can someone give me an example about how to define a new type of struct in a class in C++.
Thanks.
86
votes
5
answers
24k
views
What does stream mean? What are its characteristics?
C++ and C# both use the word stream to name many classes.
C++: iostream, istream, ostream, stringstream, ostream_iterator, istream_iterator...
C#: Stream, FileStream,MemoryStream, BufferedStream...
...
84
votes
9
answers
52k
views
C++: Can a struct inherit from a class?
I am looking at the implementation of an API that I am using.
I noticed that a struct is inheriting from a class and I paused to ponder on it...
First, I didn't see in the C++ manual I studied ...
83
votes
6
answers
300k
views
Pointer to incomplete class type is not allowed
For some reason I cannot use functions attached to the object I want to use. I added a comment to the line that is not working. As an error I get "Error; pointer to incomplete class type is not ...
81
votes
8
answers
224k
views
Struct with template variables in C++
I'm playing around with templates. I'm not trying to reinvent the std::vector, I'm trying to get a grasp of templateting in C++.
Can I do the following?
template <typename T>
typedef struct{
...
81
votes
3
answers
5k
views
What does "class :" mean in C++?
I've never seen it before. I thought it was a typo for "::sample", but when I saw it actually compiles I was very confused. Can anyone help me find out please? I don't think it's a goto label.
void f(...
80
votes
7
answers
39k
views
Will an 'empty' constructor or destructor do the same thing as the generated one?
Suppose we have a (toy) C++ class such as the following:
class Foo {
public:
Foo();
private:
int t;
};
Since no destructor is defined, a C++ compiler should create one ...
79
votes
6
answers
66k
views
Why can't we declare a namespace within a class?
Declaring a class within a class is valid. (Nested classes)
Declaring a namespace within a class is invalid.
The question is: is there any good reason (other than c++ grammar/syntax problems) to ...
78
votes
3
answers
78k
views
default visibility of C++ class/struct members
In C++, why is private the default visibility for members of classes, but public for structs?
77
votes
3
answers
87k
views
The compiler is complaining about my default parameters?
I'm having trouble with this piece of code , after i took this class from the main.cpp file and splitted it in to .h and .cpp the compiler started complaining about the default parameters i was using ...
69
votes
3
answers
110k
views
inline function members inside a class
I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save time for the compilation. ...
69
votes
8
answers
47k
views
Defining an object without calling its constructor in C++
In C++, I want to define an object as a member of a class like this:
Object myObject;
However doing this will try to call it's parameterless constructor, which doesn't exist. However I need the ...
67
votes
5
answers
197k
views
C++ variable has initializer but incomplete type?
I am trying to compile 2 classes in C++ with the following command:
g++ Cat.cpp Cat_main.cpp -o Cat
But I receive the following error:
Cat_main.cpp:10:10: error: variable ‘Cat Joey’ has initializer ...
67
votes
4
answers
97k
views
Correct way to initialize std::vector data member
// Method One
class ClassName
{
public:
ClassName() : m_vecInts() {}
private:
std::vector<int> m_vecInts;
};
// Method Two
class ClassName
{
public:
ClassName() {} // do ...
67
votes
4
answers
376k
views
c++ "Incomplete type not allowed" error accessing class reference information (Circular dependency with forward declaration)
Had some issues in my code recently surrounding what I now know of as a Circular dependency. In short there are two classes, Player and Ball, which both need to use information from the other. Both at ...
66
votes
12
answers
164k
views
Checking if a variable is initialized
Seems like this would be a duplicate, but maybe it is just so obvious it hasn't been asked...
Is this the proper way of checking if a variable (not pointer) is initialized in a C++ class?
class ...
65
votes
3
answers
48k
views
How can I store a lambda expression as a field of a class in C++11?
I'd like to create a class where the client can store a lambda expression like []() -> void {} as a field of the class, but I can't figure out how to do so. One answer suggested using decltype, ...
63
votes
9
answers
110k
views
What is the difference between a concrete class and an abstract class?
I am learning C++, but I am confused about abstract class and concrete class. Some real world examples would be appreciated.
63
votes
7
answers
89k
views
How to initialize static members in the header [duplicate]
Given is a class with a static member.
class BaseClass
{
public:
static std::string bstring;
};
String has obviously to be default-initialized outside of the class.
std::string BaseClass::...
63
votes
2
answers
104k
views
How to create two classes in C++ which use each other as data?
I'm looking to create two classes, each of which contains an object of the other class type. How can I do this? If I can't do this, is there a work-around, like having each class contain a pointer to ...
63
votes
6
answers
34k
views
How to define sealed class in C++?
How to stop the class to be inherited by other class.
63
votes
1
answer
125k
views
Different ways of initializing an object in c++
Imagine this class:
class Entity {
public:
int x, y;
Entity() : x(0), y(0) { }
Entity(int x, int y) : x(x), y(y) { }
}
And here are multiple ways of initializing the class with what ...
63
votes
2
answers
42k
views
What is the default value for C++ class members
What is the default values for members of a struct and members of a class in c++, and how do these rules differ (e.g. between classes/structs/primitives/etc) ? Are there circumstances where the rules ...